织梦网站识别,php网站数据库修改,哪些网站可以做团购,上传网站图片处理我们通过Requests请求url获取数据#xff0c;请求把数据返回来之后就要提取目标数据#xff0c;不同的网站返回的内容通常有多种不同的格式#xff0c;一种是 json 格式#xff0c;我们可以直接通过json.loads转换python的json对象处理。另一种 XML 格式的#xff0c;还有…我们通过Requests请求url获取数据请求把数据返回来之后就要提取目标数据不同的网站返回的内容通常有多种不同的格式一种是 json 格式我们可以直接通过json.loads转换python的json对象处理。另一种 XML 格式的还有一种最常见格式的是 HTML 文档今天就来讲讲如何从 HTML 中提取出感兴趣的数据。
BeautifulSoup 是一个用于解析 HTML 文档的 Python 库通过 BeautifulSoup你只需要用很少的代码就可以提取出 HTML 中任何感兴趣的内容此外它还有一定的 HTML 容错能力对于一个格式不完整的HTML 文档它也可以正确处理。
安装 beautifulsoup
pip install beautifulsoup4初始化对象时可以直接传递字符串或者文件句柄
soup BeautifulSoup(open(index.html))
soup BeautifulSoup(htmldata/html)支持多种解析接口
# python内置HTML解析
BeautifulSoup(markup, html.parser)
# lxml语言支持HTML解析
BeautifulSoup(markup, lxml)
# 解析XML引擎
BeautifulSoup(markup, xml)
# 解析HTML5引擎
BeautifulSoup(markup, html5lib)
自动添加和补全标签
下面是一段不规范的html缺少闭合标签
html_doc
htmlheadtitleThe Dormouses story/title/head
body
p classtitlebThe Dormouses story/b/pp classstoryOnce upon a time there were three little sisters; and their names were
a hrefhttp://example.com/elsie classsister idlink1Elsie/a,
a hrefhttp://example.com/lacie classsister idlink2Lacie/a and
a hrefhttp://example.com/tillie classsister idlink3Tillie/a;
and they lived at the bottom of a well./pp classstory.../p它由很多标签Tag组成比如 html、head、title等等都是标签一个标签对构成一个节点比如 …是一个根节点节点之间存在某种关系比如p之间互为邻居他们是相邻的兄弟sibling节点p 是 body 的直接子children节点还是 html 的子孙descendants节点body 是 p 的父parent节点html 是 p 的祖辈parents节点嵌套在标签之间的字符串是该节点下的一个特殊子节点比如title文本内容“The Dormouse’sstory”也是一个节点只不过没名字。 from bs4 import BeautifulSoup
soup BeautifulSoup(html_doc, html.parser)
print(soup.prettify())prettify()标准缩进格式的输出。输出内容如下 htmlheadtitleThe Dormouses story/title/headbodyp classtitlebThe Dormouses story/b/pp classstoryOnce upon a time there were three little sisters; and their names werea classsister hrefhttp://example.com/elsie idlink1Elsie/a,a classsister hrefhttp://example.com/lacie idlink2Lacie/aanda classsister hrefhttp://example.com/tillie idlink2Tillie/a; and they lived at the bottom of a well./pp classstory.../p/body/htmlbs4获取标签及内容示例 # title标签
soup.title
# titleThe Dormouses story/title# title标签名称
soup.title.name
# title# # title标签的文本字符内容
soup.title.string
# The Dormouses story# title标签父节点名称
soup.title.parent.name
# head# 从前向后找到html孙节点第一个p节点
soup.p
# p classtitlebThe Dormouses story/b/p# p节点的class属性
soup.p[class]
# [title]# 进栈出栈的方式找到第一个a标签
soup.a
# a classsister hrefhttp://example.com/elsie idlink1Elsie/a# p节点的href属性
soup.a[href]
# http://example.com/elsiesoup.find_all(a)
# 同上
soup.find_all(p)[1].find_all(a)# [a classsister hrefhttp://example.com/elsie idlink1Elsie/a,
# a classsister hrefhttp://example.com/lacie idlink2Lacie/a,
# a classsister hrefhttp://example.com/tillie idlink3Tillie/a]soup.find(idlink3)
# a classsister hrefhttp://example.com/tillie idlink3Tillie/a遍历文档树
从根节点 html 标签开始遍历元素进栈出栈直到找到目标元素为止。
BeatifulSoup 将 HTML 抽象成为 4 类主要的数据类型:
Tag每个标签节点就是一个Tag对象NavigableString包裹在Tag对象里的文本字符串BeautifulSoup 对象代表要解析的整个Comment注释对象 type(soup)
# class bs4.BeautifulSoup
type(soup.p)
# class bs4.element.Tag
# type(soup.p.string)
class bs4.element.NavigableStringTag标签
每个 Tag 都有一个名字它对应 HTML 的标签名称。
soup.p.name
# p标签有属性属性的访问方式和字典是类似的它返回一个列表对象或字符串。
soup.p[class]
# [title]soup.a[href]
# http://example.com/elsieNavigableString 获取标签中的内容直接使用 .stirng 即可获取它是一个 NavigableString 对象 。
soup.p.string
# The Dormouses story
type(soup.p.string)
bs4.element.NavigableString搜索文档树
搜索文档树是通过指定标签名来搜索元素还可以通过指定标签的属性值来精确定位某个节点元素最常用的两个方法就是 find 和 find_all。这两个方法在 BeatifulSoup 和 Tag 对象上都可以被调用。
find_all()方法
find_all( name , attrs , recursive , text , **kwargs )第一个参数 name 是标签节点的名字。
# 所有p标签
soup.find_all(p)# [p classtitlebThe Dormouses story/b/p,
# p classstoryOnce upon a time there were three little sisters; and their names were
# a classsister hrefhttp://example.com/elsie idlink1Elsie/a,
# a classsister hrefhttp://example.com/lacie idlink2Lacie/a and
# a classsister hrefhttp://example.com/tillie idlink3Tillie/a;
# and they lived at the bottom of a well./p,
# p classstory.../p]第二个参数是标签的class属性值
soup.find_all(p,title)
# 同上
soup.find_all(p,class_ title)# [p classtitlebThe Dormouses story/b/p]kwargs 是标签的属性名值对。
import re
# 支持使用标签属性
soup.find_all(hrefhttp://example.com/lacie)
soup.find_all(idlink2)# 支持使用正则
soup.find_all(hrefre.compile(lacie))# [a classsister hrefhttp://example.com/lacie idlink2Lacie/a]# 支持使用布尔类型
soup.find_all(a,idTrue)遍历和搜索相结合先定位到 body 标签再从 body 中找 a 标签.。
soup.body.find_all(a,idTrue)find()方法
find 方法跟 find_all 类似唯一不同的地方是它返回的单个 Tag 对象而非列表如果没找到匹配的节点则返回 None。如果匹配多个 Tag只返回第0个。
soup.body.find(a)
# a classsister hrefhttp://example.com/elsie idlink1Elsie/aget_text()方法 获取标签里面内容除了可以使用 .string 之外还可以使用 get_text 方法不同的地方在于前者返回的一个 NavigableString 对象后者返回的是 字符串。
soup.body.find(a).get_text()
# Elsie实际场景中我们一般使用 get_text 方法获取标签中的内容。
总结
通过beautifulsoup我们能够解析大部分静态html网页遍历和搜索组合方式定位html的标签并获取相应标签的内容。