美文网首页大数据 爬虫Python AI Sql
Python爬虫-常用数据提取方式

Python爬虫-常用数据提取方式

作者: 杰伊_约翰 | 来源:发表于2019-08-03 10:27 被阅读0次

Python爬虫,常用的请求的方式又urllib和第三方库requests,获取标签或字段的方式不同,有xpath,bs4,正则;这几个的话正则肯定是执行效率比较高的,当然编写过程也比较繁琐,一般都是在用xpath和bs4,正则的话是一个基础,最好是掌握一下。

那么将逐一来介绍这些模块的使用

安装

pip3 install requests
pip3 install bs4
pip3 install lxml

正则

提起正则,你要想起
. * ? \d \D \w \W \s \S 这些是单字符匹配

使用

find_all方法查询出所有

import re
pattern = re.comple('<li.*?>(.*?)</li>') #  这是在匹配所有li标签内的文本,要匹配的文本就用小括号包括起来
result = re.find_all(html,pattern) # html获取的网页源码,pattern刚刚写的正则规则,find_all查询出所有

xpath

XPath (XML Path Language) 是一门在 XML 文档中查找信息的语言,可用来在 XML 文档中对元素和属性进行遍历。

基本语法如下:

nodename选取此节点的所有子节点。
/ 从根节点选取。
// 从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置。
. 选取当前节点。
.. 选取当前节点的父节点。
@ 选取属性。

具体使用

def get_img(self,url): '''获取一个帖子里面的所有图片''' 
    html = self.parse_url(url) #返回elemet累心的html,具有xpath方法 
    img_list = html.xpath('//div[@data-class="BDE_Image"]/@data-url') 
    img_list = [i.split("src=")[-1] for i in img_list] #提取图片的url 
    img_list = [requests.utils.unquote(i) for i in img_list] 
    return img_list

bs4

bs4库的简单使用
这里我们先简单的讲解一下bs4库的使用,暂时不去考虑如何从web上抓取网页,假设我们需要爬取的html是如下这么一段:
//下面的一段HTML代码将作为例子被多次用到.这是 爱丽丝梦游仙境的 的一段内容(以后内容中简称为 爱丽丝 的文档):

<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
http://example.com/elsie" class="sister" id="link1">Elsie,
http://example.com/lacie" class="sister" id="link2">Lacie and
http://example.com/tillie" class="sister" id="link3">Tillie;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
</html>

下面我们开始用bs4库解析这一段html网页代码。
#导入bs4模块
from bs4 import BeautifulSoup
#做一个美味汤
soup = BeautifulSoup(html,'html.parser')
#输出结果
print(soup.prettify())

'''
OUT:

# <html>
#  <head>
#   <title>
#    The Dormouse's story
#   </title>
#  </head>
#  <body>
#   <p class="title">
#    <b>
#     The Dormouse's story
#    </b>
#   </p>
#   <p class="story">
#    Once upon a time there were three little sisters; and their names were
#    <a class="sister" href="http://example.com/elsie" id="link1">
#     Elsie
#    </a>
#    ,
#    <a class="sister" href="http://example.com/lacie" id="link2">
#     Lacie
#    </a>
#    and
#    <a class="sister" href="http://example.com/tillie" id="link2">
#     Tillie
#    </a>
#    ; and they lived at the bottom of a well.
#   </p>
#   <p class="story">
#    ...
#   </p>
#  </body>
# </html>
'''

可以看到bs4库将网页文件变成了一个soup的类型,事实上,bs4库 是解析、遍历、维护、“标签树“的功能库。
通俗一点说就是: bs4库把html源代码重新进行了格式化,从而方便我们对其中的节点、标签、属性等进行操作。
下面是几个简单的浏览结构化数据的方式 :
请仔细观察最前面的html文件

# 找到文档的title
soup.title
# <title>The Dormouse's story</title>

#title的name值
soup.title.name
# u'title'

#title中的字符串String
soup.title.string
# u'The Dormouse's story'

#title的父亲节点的name属性
soup.title.parent.name
# u'head'

#文档的第一个找到的段落
soup.p
# <p class="title"><b>The Dormouse's story</b></p>

#找到的p的class属性值
soup.p['class']
# u'title'

#找到a标签
soup.a
# http://example.com/elsie" id="link1">Elsie

#找到所有的a标签
soup.find_all('a')
# [http://example.com/elsie" id="link1">Elsie,
#  http://example.com/lacie" id="link2">Lacie,
#  http://example.com/tillie" id="link3">Tillie]

#找到id值等于3的a标签
soup.find(id="link3")
# http://example.com/tillie" id="link3">Tillie

通过上面的例子 我们知道bs4库是这样理解一个html源文件的:

首先 把html源文件转换为soup类型
接着 从中通过特定的方式抓取内容

更高级点的用法?

从文档中找到所有标签的链接:

#发现了没有,find_all方法返回的是一个可以迭代的列表
for link in soup.find_all('a'):
    print(link.get('href'))
    # http://example.com/elsie
    # http://example.com/lacie
    # http://example.com/tillie


从文档中获取所有文字内容:

#我们可以通过get_text 方法 快速得到源文件中的所有text内容。
print(soup.get_text())
# The Dormouse's story
#
# The Dormouse's story
#
# Once upon a time there were three little sisters; and their names were
# Elsie,
# Lacie and
# Tillie;
# and they lived at the bottom of a well.
#
# ...

相关文章

网友评论

    本文标题:Python爬虫-常用数据提取方式

    本文链接:https://www.haomeiwen.com/subject/pyoddctx.html