淘宝网站建设与经营论文,工业产品设计网,增城手机网站建设,wordpress 主页文章分类1. 介绍
BeautifulSoup是用来从HTML、XML文档中提取数据的一个python库#xff0c;安装如下:
pip install beautifulsoup4
它支持多种解析器#xff0c;包括python标准库、lxml HTML解析器、lxml XML解析器、html5lib等。结合稳定性和速度#xff0c;这里推荐使用lxml HT…1. 介绍
BeautifulSoup是用来从HTML、XML文档中提取数据的一个python库安装如下:
pip install beautifulsoup4
它支持多种解析器包括python标准库、lxml HTML解析器、lxml XML解析器、html5lib等。结合稳定性和速度这里推荐使用lxml HTML解析器。安装
pip install lxml
如果lxml不能正确解析内容这是可以使用html5lib。安装
pip install html5lib
2. 使用
2.1 一般流程
beautifulsoup的使用流程一般包括1.导入库 2.实例化对象 3.调用对象
在实例化对象的时候要传入两个参数一个是待解析的html或xml字符串(markup)另一个是选择的解析器(features)。如果未指定解析器会调用默认解析器。 from bs4 import BeautifulSoup soup BeautifulSoup(html_str, lxml 2.2 选择器
要定位到指定的地方提取想要的数据就需要借助选择器进行定位。beautifulsoup有三种选择器节点选择器、方法选择器、css选择器。
节点选择器的使用
选取标签节点选择器是通过HTML标签进行定位的使用方法是实例化soup对象后直接加.tagtag就是html标签名。 import requests
from bs4 import BeautifulSoupurl https://www.baidu.com
response requests.get(urlurl)
response.encoding response.apparent_encoding
soup BeautifulSoup(response.text, lxml)
print(soup.a) # out: a classmnav hrefhttp://news.baidu.com nametj_trnews新闻/a 这样选取的缺点是如果有多个相同标签只会提取第一个标签其他的被忽略。
获取标签信息在获取到的标签的基础上还可以进一步获取标签名、属性、值 print(soup.a)
print(soup.a.name)
print(soup.a.attrs)
print(soup.a.string)# out: a classmnav hrefhttp://news.baidu.com nametj_trnews新闻/a a {href: http://news.baidu.com, name: tj_trnews, class: [mnav]} 新闻 嵌套选择如果一个标签里还包含一个子标签我们还可以通过嵌套选择的方法取出子标签 print(soup.head.title)
print(soup.head.title.string) # out: title百度一下你就知道/title 百度一下你就知道 子标签列举 有两种方式包括contents(返回列表)、children(返回可迭代对象)。该方法会把所有子标签取出这样我们就可以拿到指定位置的子标签。 from bs4 import BeautifulSoup
hh trthIP/ththPORT/thth匿名度/thth类型/thth位置/thth响应速度/thth录取时间divtest/div/th/tr
soup BeautifulSoup(hh, lxml)
print(soup.tr.contents)
print(soup.tr.children)
for index, child in enumerate(soup.tr.children):print(f({index}):{child}) # out: [thIP/th, thPORT/th, th匿名度/th, th类型/th, th位置/th, th响应速度/th, th录取时间divtest/div/th] list_iterator object at 0x000001C4BF512E00 (0):thIP/th (1):thPORT/th (2):th匿名度/th (3):th类型/th (4):th位置/th (5):th响应速度/th (6):th录取时间divtest/div/th 父标签列举.parent会列出标签的父标签.parents会列出所有的祖先元素并整合在可迭代对象里。 from bs4 import BeautifulSoup
hh trthIP/ththPORT/thth匿名度/thth类型/thth位置/thth响应速度/thth录取时间divtest/div/th/tr
soup BeautifulSoup(hh, lxml)
print(soup.div.parent)
print(soup.div.parents)
for index, parents in enumerate(soup.tr.parents):print(f({index}):{parents}) # out th录取时间 divtest/div /th generator object PageElement.parents at 0x0000028479C541C0 (0):bodytr thIP/th thPORT/th th匿名度/th th类型/th th位置/th th响应速度/th th录取时间 divtest/div /th /tr/body (1):htmlbodytr thIP/th thPORT/th th匿名度/th th类型/th th位置/th th响应速度/th th录取时间 divtest/div /th /tr/body/html 兄弟节点选取 next_sibling获取下一个兄弟节点next_siblings获取后续所有兄弟节点previous_sibling获取前一个兄弟节点previous_siblings获取前面所有兄弟节点。 from bs4 import BeautifulSoup
hh trthIP/ththPORT/thth匿名度/thth类型/thth位置/thth响应速度/thth录取时间divtest/div/th/tr
soup BeautifulSoup(hh, lxml)
print(soup.th.next_sibling)
print(soup.th.next_siblings)
print(list(soup.th.next_siblings)[1].previous_sibling)
print(soup.th.previous_siblings) # out thPORT/th generator object PageElement.next_siblings at 0x000001EB1EEB4280 thPORT/th generator object PageElement.previous_siblings at 0x000001EB1EEB4280 CSS选择器的使用
使用CSS选择器只需要调用select()方法结合CSS语法即可定位到元素。
在css语法中我们可以使用类选择器、id选择器、标签选择器、以及混合使用来定位。 from bs4 import BeautifulSoup
hh trthIP/ththPORT/thth匿名度/thth类型/thth位置/thth响应速度/thth录取时间divtest/div/th/tr
soup BeautifulSoup(hh, lxml)
print(soup.select(th)) # out: [thIP/th, thPORT/th, th匿名度/th, th类型/th, th位置/th, th响应速度/th, th录取时间divtest/div/th] 我们只需传入css同样的定位语法即可 返回结果为列表。
获取节点的属性和文本用法与前面的节点选择器相同。
方法选择器的使用
使用方法选择器主要使用其中的find()和findALL()方法finaALL方法需要传入的参数有nameattrstextkwargs。
name为定位条件可以为标签名如a也可以是多个标签名组成的列表还可以是正则表达式。 from bs4 import BeautifulSoup
hh trthIP/ththPORT/thth匿名度/thth类型/thth位置/thth响应速度/thth录取时间divtest/div/th/tr
soup BeautifulSoup(hh, lxml)
print(soup.findAll(nameth)) # out: [thIP/th, thPORT/th, th匿名度/th, th类型/th, th位置/th, th响应速度/th, th录取时间divtest/div/th] attrs为属性可根据属性定位标签。 from bs4 import BeautifulSoup
hh trthIP/ththPORT/thth匿名度/thth类型/thth位置/thth响应速度/thth录取时间div idtetest/div/th/tr
soup BeautifulSoup(hh, lxml)
print(soup.findAll(attrs{id:te}))# out: [div idtetest/div] string为文本可以搜索文本信息常与其他name或attr混用来获取标签。 from bs4 import BeautifulSoup
hh trthIP/ththPORT/thth匿名度/thth类型/thth位置/thth响应速度/thth录取时间div idtetest/div/th/tr
soup BeautifulSoup(hh, lxml)
print(soup.findAll(nameth, string匿名度)) # out: [th匿名度/th] kwargs为关键字参数比attr使用更方便传入指定关键字参数以定位标签。class与python中的关键字冲突了所以需要加下划线避免冲突。 from bs4 import BeautifulSoup
hh trthIP/ththPORT/thth匿名度/thth类型/thth位置/thth响应速度/thth录取时间div classtetest/div/th/tr
soup BeautifulSoup(hh, lxml)
print(soup.findAll(class_te))# out: [div classtetest/div] limit为限制参数限制返回结果的数量 from bs4 import BeautifulSoup
hh trthIP/ththPORT/thth匿名度/thth类型/thth位置/thth响应速度/thth录取时间div classtetest/div/th/tr
soup BeautifulSoup(hh, lxml)
print(soup.findAll(nameth, limit3)) # out: [thIP/th, thPORT/th, th匿名度/th] find方法 相比于find_all()方法除了limit参数不能用,其他参数均与find_all()方法相同不过find方法只会返回一个值而不是像find_all返回所有值。