加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

这是我见过最牛逼,最全面的Beautiful Soup 4.2 教程!没有之一

发布时间:2020-12-17 01:13:27 所属栏目:Python 来源:网络整理
导读:p style="color:rgb(34,34,34);font-family:'PingFang SC','Hiragino Sans GB','Microsoft YaHei','WenQuanYi Micro Hei','Helvetica Neue',Arial,sans-serif;background-color:rgb(255,255,255);"span style="font-weight:700;"进群:125240963 ? ?即可获取

<p style="color:rgb(34,34,34);font-family:'PingFang SC','Hiragino Sans GB','Microsoft YaHei','WenQuanYi Micro Hei','Helvetica Neue',Arial,sans-serif;background-color:rgb(255,255,255);"><span style="font-weight:700;">进群:125240963 ? ?即可获取数十套PDF!

<p style="color:rgb(34,255);">Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你节省数小时甚至数天的工作时间.

<p style="color:rgb(34,255);">这篇文档介绍了BeautifulSoup4中所有主要特性,并且有小例子.让我来向你展示它适合做什么,如何工作,怎样使用,如何达到你想要的效果,和处理异常情况.

<p style="color:rgb(34,255);">文档中出现的例子在Python2.7和Python3.2中的执行结果相同

<p style="color:rgb(34,255);">你可能在寻找 Beautiful Soup3 的文档,Beautiful Soup 3 目前已经停止开发,我们推荐在现在的项目中使用Beautiful Soup 4,移植到BS4

<p style="color:rgb(34,255);">寻求帮助

<p style="color:rgb(34,255);">如果你有关于BeautifulSoup的问题,可以发送邮件到 讨论组 .如果你的问题包含了一段需要转换的HTML代码,那么确保你提的问题描述中附带这段HTML文档的 代码诊断 [1]

<h1 style="font-size:24px;line-height:32px;color:rgb(34,255);">快速开始<p style="color:rgb(34,255);">下面的一段HTML代码将作为例子被多次用到.这是 爱丽丝梦游仙境的 的一段内容(以后内容中简称为 爱丽丝 的文档):

<p style="color:rgb(34,255);">html_doc = """

<p style="color:rgb(34,255);">The Dormouse's story

<p style="color:rgb(34,255);">

<p style="color:rgb(34,255);"><p class="title">The Dormouse's story

<p style="color:rgb(34,255);"><p class="story">Once upon a time there were three little sisters; and their names were

<p style="color:rgb(34,255);"><a href="http://example.com/elsie" class="sister" id="link1">Elsie,

<p style="color:rgb(34,255);"><a href="http://example.com/lacie" class="sister" id="link2">Lacie and

<p style="color:rgb(34,255);"><a href="http://example.com/tillie" class="sister" id="link3">Tillie;

<p style="color:rgb(34,255);">and they lived at the bottom of a well.

<p style="color:rgb(34,255);"><p class="story">...

<p style="color:rgb(34,255);">"""

<p style="color:rgb(34,255);">使用BeautifulSoup解析这段代码,能够得到一个 BeautifulSoup 的对象,并能按照标准的缩进格式的结构输出:

<p style="color:rgb(34,255);"><span style="font-weight:700;">from?<span style="font-weight:700;">bs4?<span style="font-weight:700;">import?BeautifulSoup

<p style="color:rgb(34,255);">soup = BeautifulSoup(html_doc)

<p style="color:rgb(34,255);"><span style="font-weight:700;">print(soup.prettify())

<p style="color:rgb(34,255);">#

<p style="color:rgb(34,255);">#

<p style="color:rgb(34,255);"># </p><p style="color:rgb(34,255);"># The Dormouse's story</p><p style="color:rgb(34,255);">#

<p style="color:rgb(34,255);">#

<p style="color:rgb(34,255);">#

<p style="color:rgb(34,255);"># <p class="title">

<p style="color:rgb(34,255);">#

<p style="color:rgb(34,255);">#

<p style="color:rgb(34,255);">#

<p style="color:rgb(34,255);"># <p class="story">

<p style="color:rgb(34,255);"># Once upon a time there were three little sisters; and their names were

<p style="color:rgb(34,255);"># <a class="sister" href="http://example.com/elsie" id="link1">

<p style="color:rgb(34,255);"># Elsie

<p style="color:rgb(34,255);">#

<p style="color:rgb(34,255);">#,255);"># <a class="sister" href="http://example.com/lacie" id="link2">

<p style="color:rgb(34,255);"># Lacie

<p style="color:rgb(34,255);"># and

<p style="color:rgb(34,255);"># <a class="sister" href="http://example.com/tillie" id="link2">

<p style="color:rgb(34,255);"># Tillie

<p style="color:rgb(34,255);"># ; and they lived at the bottom of a well.

<p style="color:rgb(34,255);"># ...

<p style="color:rgb(34,255);">#

<p style="color:rgb(34,255);">#

<p style="color:rgb(34,255);">几个简单的浏览结构化数据的方法:

<p style="color:rgb(34,255);">soup.title

<p style="color:rgb(34,255);"># The Dormouse's story

<p style="color:rgb(34,255);">soup.title.name

<p style="color:rgb(34,255);"># u'title'

<p style="color:rgb(34,255);">soup.title.string

<p style="color:rgb(34,255);"># u'The Dormouse's story'

<p style="color:rgb(34,255);">soup.title.parent.name

<p style="color:rgb(34,255);"># u'head'

<p style="color:rgb(34,255);">soup.p

<p style="color:rgb(34,255);"># <p class="title">The Dormouse's story

<p style="color:rgb(34,255);">soup.p['class']

<p style="color:rgb(34,255);">soup.a

<p style="color:rgb(34,255);"># <a class="sister" href="http://example.com/elsie" id="link1">Elsie

<p style="color:rgb(34,255);">soup.find_all('a')

<p style="color:rgb(34,255);"># [<a class="sister" href="http://example.com/elsie" id="link1">Elsie,255);"># <a class="sister" href="http://example.com/lacie" id="link2">Lacie,255);"># <a class="sister" href="http://example.com/tillie" id="link3">Tillie]

<p style="color:rgb(34,255);">soup.find(id="link3")

<p style="color:rgb(34,255);"># <a class="sister" href="http://example.com/tillie" id="link3">Tillie

<p style="color:rgb(34,255);">从文档中找到所有标签的链接:

<p style="color:rgb(34,255);"><span style="font-weight:700;">for?link?<span style="font-weight:700;">in?soup.find_all('a'):

<p style="color:rgb(34,255);"><span style="font-weight:700;">print(link.get('href'))

<p style="color:rgb(34,255);"># http://example.com/elsie

<p style="color:rgb(34,255);"># http://example.com/lacie

<p style="color:rgb(34,255);"># http://example.com/tillie

<p style="color:rgb(34,255);">从文档中获取所有文字内容:

<p style="color:rgb(34,255);"><span style="font-weight:700;">print(soup.get_text())

<p style="color:rgb(34,255);">#

<p style="color:rgb(34,255);"># Elsie,255);"># Lacie and

<p style="color:rgb(34,255);"># Tillie;

<p style="color:rgb(34,255);"># and they lived at the bottom of a well.

<p style="color:rgb(34,255);">这是你想要的吗?别着急,还有更好用的

<h1 style="font-size:24px;line-height:32px;color:rgb(34,255);">安装 Beautiful Soup<p style="color:rgb(34,255);">如果你用的是新版的Debain或ubuntu,那么可以通过系统的软件包管理来安装:

<p style="color:rgb(34,255);">$ apt-get install Python-bs4

<p style="color:rgb(34,255);">Beautiful Soup 4 通过PyPi发布,所以如果你无法使用系统包管理安装,那么也可以通过 easy_install 或 pip 来安装.包的名字是 beautifulsoup4,这个包兼容Python2和Python3.

<p style="color:rgb(34,255);">$ easy_install beautifulsoup4

<p style="color:rgb(34,255);">$ pip install beautifulsoup4

<p style="color:rgb(34,255);">(在PyPi中还有一个名字是 BeautifulSoup 的包,但那可能不是你想要的,那是 Beautiful Soup3 的发布版本,因为很多项目还在使用BS3,所以 BeautifulSoup 包依然有效.但是如果你在编写新项目,那么你应该安装的beautifulsoup4 )

<p style="color:rgb(34,255);">如果你没有安装 easy_install 或 pip,那你也可以 下载BS4的源码,然后通过setup.py来安装.

<p style="color:rgb(34,255);">$ Python setup.py install

<p style="color:rgb(34,255);">如果上述安装方法都行不通,Beautiful Soup的发布协议允许你将BS4的代码打包在你的项目中,这样无须安装即可使用.

<p style="color:rgb(34,255);">作者在Python2.7和Python3.2的版本下开发Beautiful Soup,理论上Beautiful Soup应该在所有当前的Python版本中正常工作

<p style="color:rgb(34,255);">安装完成后的问题

<p style="color:rgb(34,255);">Beautiful Soup发布时打包成Python2版本的代码,在Python3环境下安装时,会自动转换成Python3的代码,如果没有一个安装的过程,那么代码就不会被转换.

<p style="color:rgb(34,255);">如果代码抛出了 ImportError 的异常: “No module named HTMLParser”,这是因为你在Python3版本中执行Python2版本的代码.

<p style="color:rgb(34,255);">如果代码抛出了 ImportError 的异常: “No module named html.parser”,这是因为你在Python2版本中执行Python3版本的代码.

<p style="color:rgb(34,255);">如果遇到上述2种情况,最好的解决方法是重新安装BeautifulSoup4.

<p style="color:rgb(34,255);">如果在ROOT_TAG_NAME = u’[document]’代码处遇到 SyntaxError “Invalid syntax”错误,需要将把BS4的Python代码版本从Python2转换到Python3. 可以重新安装BS4:

<p style="color:rgb(34,255);">$ Python3 setup.py install

<p style="color:rgb(34,255);">或在bs4的目录中执行Python代码版本转换脚本

<p style="color:rgb(34,255);">$ 2to3-3.2 -w bs4

<p style="color:rgb(34,255);">安装解析器

<p style="color:rgb(34,255);">Beautiful Soup支持Python标准库中的HTML解析器,还支持一些第三方的解析器,其中一个是 lxml .根据操作系统不同,可以选择下列方法来安装lxml:

<p style="color:rgb(34,255);">$ apt-get install Python-lxml

<p style="color:rgb(34,255);">$ easy_install lxml

<p style="color:rgb(34,255);">$ pip install lxml

<p style="color:rgb(34,255);">另一个可供选择的解析器是纯Python实现的 html5lib,html5lib的解析方式与浏览器相同,可以选择下列方法来安装html5lib:

<p style="color:rgb(34,255);">$ apt-get install Python-html5lib

<p style="color:rgb(34,255);">$ easy_install html5lib

<p style="color:rgb(34,255);">$ pip install html5lib

<p style="color:rgb(34,255);">下表列出了主要的解析器,以及它们的优缺点:

<ul style="list-style:square outside;color:rgb(34,255);"><li style="margin-left:0px;list-style:inherit;">解析器使用方法优势劣势Python标准库BeautifulSoup(markup,"html.parser")Python的内置标准库<li style="margin-left:0px;list-style:inherit;">执行速度适中<li style="margin-left:0px;list-style:inherit;">文档容错能力强<li style="margin-left:0px;list-style:inherit;">Python 2.7.3 or 3.2.2)前 的版本中文档容错能力差<li style="margin-left:0px;list-style:inherit;">lxml HTML 解析器BeautifulSoup(markup,"lxml")速度快<li style="margin-left:0px;list-style:inherit;">文档容错能力强<li style="margin-left:0px;list-style:inherit;">需要安装C语言库<p style="color:rgb(34,255);">lxml XML 解析器BeautifulSoup(markup,["lxml","xml"])

<p style="color:rgb(34,255);">BeautifulSoup(markup,"xml")

<ul style="list-style:square outside;color:rgb(34,255);"><li style="margin-left:0px;list-style:inherit;">速度快<li style="margin-left:0px;list-style:inherit;">唯一支持XML的解析器<li style="margin-left:0px;list-style:inherit;">需要安装C语言库<li style="margin-left:0px;list-style:inherit;">html5libBeautifulSoup(markup,"html5lib")最好的容错性<li style="margin-left:0px;list-style:inherit;">以浏览器的方式解析文档<li style="margin-left:0px;list-style:inherit;">生成HTML5格式的文档<li style="margin-left:0px;list-style:inherit;">速度慢<li style="margin-left:0px;list-style:inherit;">不依赖外部扩展<p style="color:rgb(34,255);">推荐使用lxml作为解析器,因为效率更高. 在Python2.7.3之前的版本和Python3中3.2.2之前的版本,必须安装lxml或html5lib,因为那些Python版本的标准库中内置的HTML解析方法不够稳定.

<p style="color:rgb(34,255);">提示: 如果一段HTML或XML文档格式不正确的话,那么在不同的解析器中返回的结果可能是不一样的,查看 解析器之间的区别 了解更多细节

<h1 style="font-size:24px;line-height:32px;color:rgb(34,255);">如何使用<p style="color:rgb(34,255);">将一段文档传入BeautifulSoup 的构造方法,就能得到一个文档的对象,可以传入一段字符串或一个文件句柄.

<p style="color:rgb(34,255);">soup = BeautifulSoup(open("index.html"))

<p style="color:rgb(34,255);">soup = BeautifulSoup("data")

<p style="color:rgb(34,255);">首先,文档被转换成Unicode,并且HTML的实例都被转换成Unicode编码

<p style="color:rgb(34,255);">BeautifulSoup("Sacré bleu!")

<p style="color:rgb(34,255);">Sacré bleu!

<p style="color:rgb(34,255);">然后,Beautiful Soup选择最合适的解析器来解析这段文档,如果手动指定解析器那么Beautiful Soup会选择指定的解析器来解析文档.(参考 解析成XML ).

<h1 style="font-size:24px;line-height:32px;color:rgb(34,255);">对象的种类<p style="color:rgb(34,255);">Beautiful Soup将复杂HTML文档转换成一个复杂的树形结构,每个节点都是Python对象,所有对象可以归纳为4种: Tag,NavigableString,BeautifulSoup,Comment .

<p style="color:rgb(34,255);">Tag

<p style="color:rgb(34,255);">Tag 对象与XML或HTML原生文档中的tag相同:

<p style="color:rgb(34,255);">soup = BeautifulSoup('<b class="boldest">Extremely bold')

<p style="color:rgb(34,255);">tag = soup.b

<p style="color:rgb(34,255);">type(tag)

<p style="color:rgb(34,255);"># <class 'bs4.element.Tag'>

<p style="color:rgb(34,255);">Tag有很多方法和属性,在 遍历文档树 和 搜索文档树 中有详细解释.现在介绍一下tag中最重要的属性: name和attributes

<p style="color:rgb(34,255);">Name

<p style="color:rgb(34,255);">每个tag都有自己的名字,通过 .name 来获取:

<p style="color:rgb(34,255);">tag.name

<p style="color:rgb(34,255);"># u'b'

<p style="color:rgb(34,255);">如果改变了tag的name,那将影响所有通过当前Beautiful Soup对象生成的HTML文档:

<p style="color:rgb(34,255);">tag.name = "blockquote"

<p style="color:rgb(34,255);">tag

<p style="color:rgb(34,255);"># <blockquote class="boldest">Extremely bold

<p style="color:rgb(34,255);">Attributes

<p style="color:rgb(34,255);">一个tag可能有很多个属性. tag <b class="boldest"> 有一个 “class” 的属性,值为 “boldest” . tag的属性的操作方法与字典相同:

<p style="color:rgb(34,255);">tag['class']

<p style="color:rgb(34,255);"># u'boldest'

<p style="color:rgb(34,255);">也可以直接”点”取属性,比如: .attrs :

<p style="color:rgb(34,255);">tag.attrs

<p style="color:rgb(34,255);"># {u'class': u'boldest'}

<p style="color:rgb(34,255);">tag的属性可以被添加,删除或修改. 再说一次,tag的属性操作方法与字典一样

<p style="color:rgb(34,255);">tag['class'] = 'verybold'

<p style="color:rgb(34,255);">tag['id'] = 1

<p style="color:rgb(34,255);"># <blockquote class="verybold" id="1">Extremely bold

<p style="color:rgb(34,255);"><span style="font-weight:700;">del?tag['class']

<p style="color:rgb(34,255);"><span style="font-weight:700;">del?tag['id']

<p style="color:rgb(34,255);">#
Extremely bold

<p style="color:rgb(34,255);"># KeyError: 'class'

<p style="color:rgb(34,255);"><span style="font-weight:700;">print(tag.get('class'))

<p style="color:rgb(34,255);"># None

<p style="color:rgb(34,255);">多值属性

<p style="color:rgb(34,255);">HTML 4定义了一系列可以包含多个值的属性.在HTML5中移除了一些,却增加更多.最常见的多值的属性是 class (一个tag可以有多个CSS的class). 还有一些属性 rel,rev,accept-charset,headers,accesskey . 在Beautiful Soup中多值属性的返回类型是list:

<p style="color:rgb(34,255);">css_soup = BeautifulSoup('<p class="body strikeout">

')

<p style="color:rgb(34,255);">css_soup.p['class']

<p style="color:rgb(34,255);"># ["body","strikeout"]

<p style="color:rgb(34,255);">css_soup = BeautifulSoup('<p class="body">

')

<p style="color:rgb(34,255);"># ["body"]

<p style="color:rgb(34,255);">如果某个属性看起来好像有多个值,但在任何版本的HTML定义中都没有被定义为多值属性,那么Beautiful Soup会将这个属性作为字符串返回

<p style="color:rgb(34,255);">id_soup = BeautifulSoup('<p id="my id">

')

<p style="color:rgb(34,255);">id_soup.p['id']

<p style="color:rgb(34,255);"># 'my id'

<p style="color:rgb(34,255);">将tag转换成字符串时,多值属性会合并为一个值

<p style="color:rgb(34,255);">rel_soup = BeautifulSoup('

Back to the <a rel="index">homepage

')

<p style="color:rgb(34,255);">rel_soup.a['rel']

<p style="color:rgb(34,255);"># ['index']

<p style="color:rgb(34,255);">rel_soup.a['rel'] = ['index','contents']

<p style="color:rgb(34,255);"><span style="font-weight:700;">print(rel_soup.p)

<p style="color:rgb(34,255);">#

Back to the <a rel="index contents">homepage

<p style="color:rgb(34,255);">如果转换的文档是XML格式,那么tag中不包含多值属性

<p style="color:rgb(34,255);">xml_soup = BeautifulSoup('<p class="body strikeout">

','xml')

<p style="color:rgb(34,255);">xml_soup.p['class']

<p style="color:rgb(34,255);"># u'body strikeout'

<p style="color:rgb(34,255);">可以遍历的字符串

<p style="color:rgb(34,255);">字符串常被包含在tag内.Beautiful Soup用 NavigableString 类来包装tag中的字符串:

<p style="color:rgb(34,255);">tag.string

<p style="color:rgb(34,255);"># u'Extremely bold'

<p style="color:rgb(34,255);">type(tag.string)

<p style="color:rgb(34,255);"># <class 'bs4.element.NavigableString'>

<p style="color:rgb(34,255);">一个 NavigableString 字符串与Python中的Unicode字符串相同,并且还支持包含在 遍历文档树 和 搜索文档树 中的一些特性. 通过 unicode() 方法可以直接将 NavigableString 对象转换成Unicode字符串:

<p style="color:rgb(34,255);">unicode_string = unicode(tag.string)

<p style="color:rgb(34,255);">unicode_string

<p style="color:rgb(34,255);">type(unicode_string)

<p style="color:rgb(34,255);"># <type 'unicode'>

<p style="color:rgb(34,255);">tag中包含的字符串不能编辑,但是可以被替换成其它的字符串,用 replace_with() 方法:

<p style="color:rgb(34,255);">tag.string.replace_with("No longer bold")

<p style="color:rgb(34,255);">#
No longer bold

<p style="color:rgb(34,255);">NavigableString 对象支持 遍历文档树 和 搜索文档树 中定义的大部分属性,并非全部.尤其是,一个字符串不能包含其它内容(tag能够包含字符串或是其它tag),字符串不支持 .contents 或 .string 属性或 find() 方法.

<p style="color:rgb(34,255);">如果想在Beautiful Soup之外使用 NavigableString 对象,需要调用 unicode() 方法,将该对象转换成普通的Unicode字符串,否则就算Beautiful Soup已方法已经执行结束,该对象的输出也会带有对象的引用地址.这样会浪费内存.

<p style="color:rgb(34,255);">BeautifulSoup

<p style="color:rgb(34,255);">BeautifulSoup 对象表示的是一个文档的全部内容.大部分时候,可以把它当作 Tag 对象,它支持 遍历文档树 和 搜索文档树 中描述的大部分的方法.

<p style="color:rgb(34,255);">因为 BeautifulSoup 对象并不是真正的HTML或XML的tag,所以它没有name和attribute属性.但有时查看它的 .name 属性是很方便的,所以 BeautifulSoup 对象包含了一个值为 “[document]” 的特殊属性 .name

<p style="color:rgb(34,255);">soup.name

<p style="color:rgb(34,255);"># u'[document]'

<p style="color:rgb(34,255);">注释及特殊字符串

<p style="color:rgb(34,255);">Tag,BeautifulSoup 几乎覆盖了html和xml中的所有内容,但是还有一些特殊对象.容易让人担心的内容是文档的注释部分:

<p style="color:rgb(34,255);">markup = ""

<p style="color:rgb(34,255);">soup = BeautifulSoup(markup)

<p style="color:rgb(34,255);">comment = soup.b.string

<p style="color:rgb(34,255);">type(comment)

<p style="color:rgb(34,255);"># <class 'bs4.element.Comment'>

<p style="color:rgb(34,255);">Comment 对象是一个特殊类型的 NavigableString 对象:

<p style="color:rgb(34,255);">comment

<p style="color:rgb(34,255);"># u'Hey,buddy. Want to buy a used parser'

<p style="color:rgb(34,255);">但是当它出现在HTML文档中时,Comment 对象会使用特殊的格式输出:

<p style="color:rgb(34,255);"><span style="font-weight:700;">print(soup.b.prettify())

<p style="color:rgb(34,255);">#

<p style="color:rgb(34,255);">Beautiful Soup中定义的其它类型都可能会出现在XML的文档中: CData,ProcessingInstruction,Declaration,Doctype .与 Comment 对象类似,这些类都是 NavigableString 的子类,只是添加了一些额外的方法的字符串独享.下面是用CDATA来替代注释的例子:

<p style="color:rgb(34,255);"><span style="font-weight:700;">from?<span style="font-weight:700;">bs4?<span style="font-weight:700;">import?CData

<p style="color:rgb(34,255);">cdata = CData("A CDATA block")

<p style="color:rgb(34,255);">comment.replace_with(cdata)

<p style="color:rgb(34,255);"># <![CDATA[A CDATA block]]>

<p style="color:rgb(34,255);">#

<h1 style="font-size:24px;line-height:32px;color:rgb(34,255);">遍历文档树<p style="color:rgb(34,255);">还拿”爱丽丝梦游仙境”的文档来做例子:

<p style="color:rgb(34,255);">通过这段例子来演示怎样从文档的一段内容找到另一段内容

<p style="color:rgb(34,255);">子节点

<p style="color:rgb(34,255);">一个Tag可能包含多个字符串或其它的Tag,这些都是这个Tag的子节点.Beautiful Soup提供了许多操作和遍历子节点的属性.

<p style="color:rgb(34,255);">注意: Beautiful Soup中字符串节点不支持这些属性,因为字符串没有子节点

<p style="color:rgb(34,255);">tag的名字

<p style="color:rgb(34,255);">操作文档树最简单的方法就是告诉它你想获取的tag的name.如果想获取 标签,只要用 soup.head :

<p style="color:rgb(34,255);">soup.head

<p style="color:rgb(34,255);"># The Dormouse's story

<p style="color:rgb(34,255);">这是个获取tag的小窍门,可以在文档树的tag中多次调用这个方法.下面的代码可以获取标签中的第一个标签:

<p style="color:rgb(34,255);">soup.body.b

<p style="color:rgb(34,255);"># The Dormouse's story

<p style="color:rgb(34,255);">通过点取属性的方式只能获得当前名字的第一个tag:

<p style="color:rgb(34,255);">如果想要得到所有的标签,或是通过名字得到比一个tag更多的内容的时候,就需要用到 Searching the tree 中描述的方法,比如: find_all()

<p style="color:rgb(34,255);">.contents 和 .children

<p style="color:rgb(34,255);">tag的 .contents 属性可以将tag的子节点以列表的方式输出:

<p style="color:rgb(34,255);">head_tag = soup.head

<p style="color:rgb(34,255);">head_tag

<p style="color:rgb(34,255);">head_tag.contents

<p style="color:rgb(34,255);">[The Dormouse's story]

<p style="color:rgb(34,255);">title_tag = head_tag.contents[0]

<p style="color:rgb(34,255);">title_tag

<p style="color:rgb(34,255);">title_tag.contents

<p style="color:rgb(34,255);"># [u'The Dormouse's story']

<p style="color:rgb(34,255);">BeautifulSoup 对象本身一定会包含子节点,也就是说标签也是 BeautifulSoup 对象的子节点:

<p style="color:rgb(34,255);">len(soup.contents)

<p style="color:rgb(34,255);"># 1

<p style="color:rgb(34,255);">soup.contents[0].name

<p style="color:rgb(34,255);"># u'html'

<p style="color:rgb(34,255);">字符串没有 .contents 属性,因为字符串没有子节点:

<p style="color:rgb(34,255);">text = title_tag.contents[0]

<p style="color:rgb(34,255);">text.contents

<p style="color:rgb(34,255);"># AttributeError: 'NavigableString' object has no attribute 'contents'

<p style="color:rgb(34,255);">通过tag的 .children 生成器,可以对tag的子节点进行循环:

<p style="color:rgb(34,255);"><span style="font-weight:700;">for?child?<span style="font-weight:700;">in?title_tag.children:

<p style="color:rgb(34,255);"><span style="font-weight:700;">print(child)

<p style="color:rgb(34,255);">.descendants

<p style="color:rgb(34,255);">.contents 和 .children 属性仅包含tag的直接子节点.例如,标签只有一个直接子节点</p><p style="color:rgb(34,255);"># [<title>The Dormouse's story]

<p style="color:rgb(34,255);">但是标签也包含一个子节点:字符串 “The Dormouse’s story”,这种情况下字符串 “The Dormouse’s story”也属于<head>标签的子孙节点. .descendants 属性可以对所有tag的子孙节点进行递归循环 [5] :</p><p style="color:rgb(34,255);"><span style="font-weight:700;">for</span>?child?<span style="font-weight:700;">in</span>?head_tag.descendants:</p><p style="color:rgb(34,255);">上面的例子中,<head>标签只有一个子节点,但是有2个子孙节点:<head>节点和<head>的子节点,BeautifulSoup 有一个直接子节点(<html>节点),却有很多子孙节点:</p><p style="color:rgb(34,255);">len(list(soup.children))</p><p style="color:rgb(34,255);">len(list(soup.descendants))</p><p style="color:rgb(34,255);"># 25</p><p style="color:rgb(34,255);">.string</p><p style="color:rgb(34,255);">如果tag只有一个 NavigableString 类型子节点,那么这个tag可以使用 .string 得到子节点:</p><p style="color:rgb(34,255);">title_tag.string</p><p style="color:rgb(34,255);">如果一个tag仅有一个子节点,那么这个tag也可以使用 .string 方法,输出结果与当前唯一子节点的 .string 结果相同:</p><p style="color:rgb(34,255);">head_tag.string</p><p style="color:rgb(34,255);">如果tag包含了多个子节点,tag就无法确定 .string 方法应该调用哪个子节点的内容,.string 的输出结果是 None :</p><p style="color:rgb(34,255);"><span style="font-weight:700;">print</span>(soup.html.string)</p><p style="color:rgb(34,255);">.strings 和 stripped_strings</p><p style="color:rgb(34,255);">如果tag中包含多个字符串 [2],可以使用 .strings 来循环获取:</p><p style="color:rgb(34,255);"><span style="font-weight:700;">for</span>?string?<span style="font-weight:700;">in</span>?soup.strings:</p><p style="color:rgb(34,255);"><span style="font-weight:700;">print</span>(repr(string))</p><p style="color:rgb(34,255);"># u"The Dormouse's story"</p><p style="color:rgb(34,255);"># u' '</p><p style="color:rgb(34,255);"># u'Once upon a time there were three little sisters; and their names were '</p><p style="color:rgb(34,255);"># u'Elsie'</p><p style="color:rgb(34,255);"># u','</p><p style="color:rgb(34,255);"># u'Lacie'</p><p style="color:rgb(34,255);"># u' and '</p><p style="color:rgb(34,255);"># u'Tillie'</p><p style="color:rgb(34,255);"># u'; and they lived at the bottom of a well.'</p><p style="color:rgb(34,255);"># u'...'</p><p style="color:rgb(34,255);">输出的字符串中可能包含了很多空格或空行,使用 .stripped_strings 可以去除多余空白内容:</p><p style="color:rgb(34,255);"><span style="font-weight:700;">for</span>?string?<span style="font-weight:700;">in</span>?soup.stripped_strings:</p><p style="color:rgb(34,255);"># u'Once upon a time there were three little sisters; and their names were'</p><p style="color:rgb(34,255);"># u'and'</p><p style="color:rgb(34,255);">全部是空格的行会被忽略掉,段首和段末的空白会被删除</p><p style="color:rgb(34,255);">父节点</p><p style="color:rgb(34,255);">继续分析文档树,每个tag或字符串都有父节点:被包含在某个tag中</p><p style="color:rgb(34,255);">.parent</p><p style="color:rgb(34,255);">通过 .parent 属性来获取某个元素的父节点.在例子“爱丽丝”的文档中,<head>标签是<title>标签的父节点:</p><p style="color:rgb(34,255);">title_tag = soup.title</p><p style="color:rgb(34,255);">title_tag.parent</p><p style="color:rgb(34,255);">文档title的字符串也有父节点:<title>标签</p><p style="color:rgb(34,255);">title_tag.string.parent</p><p style="color:rgb(34,255);">文档的顶层节点比如<html>的父节点是 BeautifulSoup 对象:</p><p style="color:rgb(34,255);">html_tag = soup.html</p><p style="color:rgb(34,255);">type(html_tag.parent)</p><p style="color:rgb(34,255);"># <class 'bs4.BeautifulSoup'></p><p style="color:rgb(34,255);">BeautifulSoup 对象的 .parent 是None:</p><p style="color:rgb(34,255);"><span style="font-weight:700;">print</span>(soup.parent)</p><p style="color:rgb(34,255);">.parents</p><p style="color:rgb(34,255);">通过元素的 .parents 属性可以递归得到元素的所有父辈节点,下面的例子使用了 .parents 方法遍历了标签到根节点的所有节点.</p><p style="color:rgb(34,255);">link = soup.a</p><p style="color:rgb(34,255);">link</p><p style="color:rgb(34,255);"><span style="font-weight:700;">for</span>?parent?<span style="font-weight:700;">in</span>?link.parents:</p><p style="color:rgb(34,255);"><span style="font-weight:700;">if</span>?parent?<span style="font-weight:700;">is</span>?None:</p><p style="color:rgb(34,255);"><span style="font-weight:700;">print</span>(parent)</p><p style="color:rgb(34,255);"><span style="font-weight:700;">else</span>:</p><p style="color:rgb(34,255);"><span style="font-weight:700;">print</span>(parent.name)</p><p style="color:rgb(34,255);"># p</p><p style="color:rgb(34,255);"># body</p><p style="color:rgb(34,255);"># html</p><p style="color:rgb(34,255);"># [document]</p><p style="color:rgb(34,255);">兄弟节点</p><p style="color:rgb(34,255);">看一段简单的例子:</p><p style="color:rgb(34,255);">sibling_soup = BeautifulSoup("<b>text1</b><c>text2</c></b>")</p><p style="color:rgb(34,255);"><span style="font-weight:700;">print</span>(sibling_soup.prettify())</p><p style="color:rgb(34,255);"># </p><p style="color:rgb(34,255);"># text1</p><p style="color:rgb(34,255);"># <c></p><p style="color:rgb(34,255);"># text2</p><p style="color:rgb(34,255);"># </c></p><p style="color:rgb(34,255);">因为<b>标签和<c>标签是同一层:他们是同一个元素的子节点,所以<b>和<c>可以被称为兄弟节点.一段文档以标准格式输出时,兄弟节点有相同的缩进级别.在代码中也可以使用这种关系.</p><p style="color:rgb(34,255);">.next_sibling 和 .previous_sibling</p><p style="color:rgb(34,255);">在文档树中,使用 .next_sibling 和 .previous_sibling 属性来查询兄弟节点:</p><p style="color:rgb(34,255);">sibling_soup.b.next_sibling</p><p style="color:rgb(34,255);"># <c>text2</c></p><p style="color:rgb(34,255);">sibling_soup.c.previous_sibling</p><p style="color:rgb(34,255);"># <b>text1</b></p><p style="color:rgb(34,255);"><b>标签有 .next_sibling 属性,但是没有 .previous_sibling 属性,因为<b>标签在同级节点中是第一个.同理,<c>标签有 .previous_sibling 属性,却没有 .next_sibling 属性:</p><p style="color:rgb(34,255);"><span style="font-weight:700;">print</span>(sibling_soup.b.previous_sibling)</p><p style="color:rgb(34,255);"><span style="font-weight:700;">print</span>(sibling_soup.c.next_sibling)</p><p style="color:rgb(34,255);">例子中的字符串“text1”和“text2”不是兄弟节点,因为它们的父节点不同:</p><p style="color:rgb(34,255);">sibling_soup.b.string</p><p style="color:rgb(34,255);"># u'text1'</p><p style="color:rgb(34,255);"><span style="font-weight:700;">print</span>(sibling_soup.b.string.next_sibling)</p><p style="color:rgb(34,255);">实际文档中的tag的 .next_sibling 和 .previous_sibling 属性通常是字符串或空白. 看看“爱丽丝”文档:</p><p style="color:rgb(34,255);"><a href="http://example.com/elsie" class="sister" id="link1">Elsie</p><p style="color:rgb(34,255);"><a href="http://example.com/lacie" class="sister" id="link2">Lacie</p><p style="color:rgb(34,255);"><a href="http://example.com/tillie" class="sister" id="link3">Tillie</p><p style="color:rgb(34,255);">如果以为第一个标签的 .next_sibling 结果是第二个标签,那就错了,真实结果是第一个标签和第二个标签之间的顿号和换行符:</p><p style="color:rgb(34,255);">link.next_sibling</p><p style="color:rgb(34,255);">第二个标签是顿号的 .next_sibling 属性:</p><p style="color:rgb(34,255);">link.next_sibling.next_sibling</p><p style="color:rgb(34,255);"># <a class="sister" href="http://example.com/lacie" id="link2">Lacie</p><p style="color:rgb(34,255);">.next_siblings 和 .previous_siblings</p><p style="color:rgb(34,255);">通过 .next_siblings 和 .previous_siblings 属性可以对当前节点的兄弟节点迭代输出:</p><p style="color:rgb(34,255);"><span style="font-weight:700;">for</span>?sibling?<span style="font-weight:700;">in</span>?soup.a.next_siblings:</p><p style="color:rgb(34,255);"><span style="font-weight:700;">print</span>(repr(sibling))</p><p style="color:rgb(34,255);"><span style="font-weight:700;">for</span>?sibling?<span style="font-weight:700;">in</span>?soup.find(id="link3").previous_siblings:</p><p style="color:rgb(34,255);"># ' and '</p><p style="color:rgb(34,255);">回退和前进</p><p style="color:rgb(34,255);">看一下“爱丽丝” 文档:</p><p style="color:rgb(34,255);">HTML解析器把这段字符串转换成一连串的事件: “打开<html>标签”,”打开一个<head>标签”,”打开一个<title>标签”,”添加一段字符串”,”关闭<title>标签”,”打开<p>标签”,等等.Beautiful Soup提供了重现解析器初始化过程的方法.</p><p style="color:rgb(34,255);">.next_element 和 .previous_element</p><p style="color:rgb(34,255);">.next_element 属性指向解析过程中下一个被解析的对象(字符串或tag),结果可能与 .next_sibling 相同,但通常是不一样的.</p><p style="color:rgb(34,255);">这是“爱丽丝”文档中最后一个标签,它的 .next_sibling 结果是一个字符串,因为当前的解析过程 [2] 因为当前的解析过程因为遇到了标签而中断了:</p><p style="color:rgb(34,255);">last_a_tag = soup.find("a",id="link3")</p><p style="color:rgb(34,255);">last_a_tag</p><p style="color:rgb(34,255);">last_a_tag.next_sibling</p><p style="color:rgb(34,255);"># '; and they lived at the bottom of a well.'</p><p style="color:rgb(34,255);">但这个标签的 .next_element 属性结果是在标签被解析之后的解析内容,不是标签后的句子部分,应该是字符串”Tillie”:</p><p style="color:rgb(34,255);">last_a_tag.next_element</p><p style="color:rgb(34,255);">这是因为在原始文档中,字符串“Tillie” 在分号前出现,解析器先进入标签,然后是字符串“Tillie”,然后关闭标签,然后是分号和剩余部分.分号与标签在同一层级,但是字符串“Tillie”会被先解析.</p><p style="color:rgb(34,255);">.previous_element 属性刚好与 .next_element 相反,它指向当前被解析的对象的前一个解析对象:</p><p style="color:rgb(34,255);">last_a_tag.previous_element</p><p style="color:rgb(34,255);">last_a_tag.previous_element.next_element</p><p style="color:rgb(34,255);">.next_elements 和 .previous_elements</p><p style="color:rgb(34,255);">通过 .next_elements 和 .previous_elements 的迭代器就可以向前或向后访问文档的解析内容,就好像文档正在被解析一样:</p><p style="color:rgb(34,255);"><span style="font-weight:700;">for</span>?element?<span style="font-weight:700;">in</span>?last_a_tag.next_elements:</p><p style="color:rgb(34,255);"><span style="font-weight:700;">print</span>(repr(element))</p><p style="color:rgb(34,255);"># <p class="story">...</p></p><p style="color:rgb(34,255);"># None</p><h1 style="font-size:24px;line-height:32px;color:rgb(34,255);">搜索文档树</h1><p style="color:rgb(34,255);">Beautiful Soup定义了很多搜索方法,这里着重介绍2个: find() 和 find_all() .其它方法的参数和用法类似,请读者举一反三.</p><p style="color:rgb(34,255);">再以“爱丽丝”文档作为例子:</p><p style="color:rgb(34,255);">使用 find_all() 类似的方法可以查找到想要查找的文档内容</p><p style="color:rgb(34,255);">过滤器</p><p style="color:rgb(34,255);">介绍 find_all() 方法前,先介绍一下过滤器的类型 [3],这些过滤器贯穿整个搜索的API.过滤器可以被用在tag的name中,节点的属性中,字符串中或他们的混合中.</p><p style="color:rgb(34,255);">字符串</p><p style="color:rgb(34,255);">最简单的过滤器是字符串.在搜索方法中传入一个字符串参数,Beautiful Soup会查找与字符串完整匹配的内容,下面的例子用于查找文档中所有的<b>标签:</p><p style="color:rgb(34,255);">soup.find_all('b')</p><p style="color:rgb(34,255);"># [<b>The Dormouse's story</b>]</p><p style="color:rgb(34,255);">如果传入字节码参数,Beautiful Soup会当作UTF-8编码,可以传入一段Unicode 编码来避免Beautiful Soup解析编码出错</p><p style="color:rgb(34,255);">正则表达式</p><p style="color:rgb(34,255);">如果传入正则表达式作为参数,Beautiful Soup会通过正则表达式的 match() 来匹配内容.下面例子中找出所有以b开头的标签,这表示<body>和<b>标签都应该被找到:</p><p style="color:rgb(34,255);"><span style="font-weight:700;">import</span>?<span style="font-weight:700;">re</span></p><p style="color:rgb(34,255);"><span style="font-weight:700;">for</span>?tag?<span style="font-weight:700;">in</span>?soup.find_all(re.compile("^b")):</p><p style="color:rgb(34,255);"><span style="font-weight:700;">print</span>(tag.name)</p><p style="color:rgb(34,255);"># b</p><p style="color:rgb(34,255);">下面代码找出所有名字中包含”t”的标签:</p><p style="color:rgb(34,255);"><span style="font-weight:700;">for</span>?tag?<span style="font-weight:700;">in</span>?soup.find_all(re.compile("t")):</p><p style="color:rgb(34,255);"># title</p><p style="color:rgb(34,255);">列表</p><p style="color:rgb(34,255);">如果传入列表参数,Beautiful Soup会将与列表中任一元素匹配的内容返回.下面代码找到文档中所有标签和<b>标签:</p><p style="color:rgb(34,255);">soup.find_all(["a","b"])</p><p style="color:rgb(34,255);"># [<b>The Dormouse's story</b>,255);"># <a class="sister" href="http://example.com/elsie" id="link1">Elsie,255);">True</p><p style="color:rgb(34,255);">True 可以匹配任何值,下面代码查找到所有的tag,但是不会返回字符串节点</p><p style="color:rgb(34,255);"><span style="font-weight:700;">for</span>?tag?<span style="font-weight:700;">in</span>?soup.find_all(True):</p><p style="color:rgb(34,255);"># head</p><p style="color:rgb(34,255);"># a</p><p style="color:rgb(34,255);">方法</p><p style="color:rgb(34,255);">如果没有合适过滤器,那么还可以定义一个方法,方法只接受一个元素参数 [4],如果这个方法返回 True 表示当前元素匹配并且被找到,如果不是则反回 False</p><p style="color:rgb(34,255);">下面方法校验了当前元素,如果包含 class 属性却不包含 id 属性,那么将返回 True:</p><p style="color:rgb(34,255);"><span style="font-weight:700;">def</span>?has_class_but_no_id(tag):</p><p style="color:rgb(34,255);"><span style="font-weight:700;">return</span>?tag.has_attr('class')?<span style="font-weight:700;">and</span>?<span style="font-weight:700;">not</span>?tag.has_attr('id')</p><p style="color:rgb(34,255);">将这个方法作为参数传入 find_all() 方法,将得到所有<p>标签:</p><p style="color:rgb(34,255);">soup.find_all(has_class_but_no_id)</p><p style="color:rgb(34,255);"># [<p class="title"><b>The Dormouse's story</b></p>,255);"># <p class="story">Once upon a time there were...</p>,255);"># <p class="story">...</p>]</p><p style="color:rgb(34,255);">返回结果中只有<p>标签没有标签,因为标签还定义了”id”,没有返回<html>和<head>,因为<html>和<head>中没有定义”class”属性.</p><p style="color:rgb(34,255);">下面代码找到所有被文字包含的节点内容:</p><p style="color:rgb(34,255);"><span style="font-weight:700;">from</span>?<span style="font-weight:700;">bs4</span>?<span style="font-weight:700;">import</span>?NavigableString</p><p style="color:rgb(34,255);"><span style="font-weight:700;">def</span>?surrounded_by_strings(tag):</p><p style="color:rgb(34,255);"><span style="font-weight:700;">return</span>?(isinstance(tag.next_element,NavigableString)</p><p style="color:rgb(34,255);"><span style="font-weight:700;">and</span>?isinstance(tag.previous_element,NavigableString))</p><p style="color:rgb(34,255);"><span style="font-weight:700;">for</span>?tag?<span style="font-weight:700;">in</span>?soup.find_all(surrounded_by_strings):</p><p style="color:rgb(34,255);"><span style="font-weight:700;">print</span>?tag.name</p><p style="color:rgb(34,255);">现在来了解一下搜索方法的细节</p><p style="color:rgb(34,255);">find_all()</p><p style="color:rgb(34,255);">find_all( name,attrs,recursive,text,*<em>kwargs )</p><p style="color:rgb(34,255);">find_all() 方法搜索当前tag的所有tag子节点,并判断是否符合过滤器的条件.这里有几个例子:</p><p style="color:rgb(34,255);">soup.find_all("title")</p><p style="color:rgb(34,255);">soup.find_all("p","title")</p><p style="color:rgb(34,255);"># [<p class="title"><b>The Dormouse's story</b></p>]</p><p style="color:rgb(34,255);">soup.find_all("a")</p><p style="color:rgb(34,255);">soup.find_all(id="link2")</p><p style="color:rgb(34,255);"># [<a class="sister" href="http://example.com/lacie" id="link2">Lacie]</p><p style="color:rgb(34,255);">soup.find(text=re.compile("sisters"))</p><p style="color:rgb(34,255);">有几个方法很相似,还有几个方法是新的,参数中的 text 和 id 是什么含义? 为什么 find_all("p","title") 返回的是CSS Class为”title”的<p>标签? 我们来仔细看一下 find_all() 的参数</p><p style="color:rgb(34,255);">name 参数</p><p style="color:rgb(34,255);">name 参数可以查找所有名字为 name 的tag,字符串对象会被自动忽略掉.</p><p style="color:rgb(34,255);">简单的用法如下:</p><p style="color:rgb(34,255);">重申: 搜索 name 参数的值可以使任一类型的 过滤器,字符窜,正则表达式,列表,方法或是 True .</p><p style="color:rgb(34,255);">keyword 参数</p><p style="color:rgb(34,255);">如果一个指定名字的参数不是搜索内置的参数名,搜索时会把该参数当作指定名字tag的属性来搜索,如果包含一个名字为 id 的参数,Beautiful Soup会搜索每个tag的”id”属性.</p><p style="color:rgb(34,255);">soup.find_all(id='link2')</p><p style="color:rgb(34,255);">如果传入 href 参数,Beautiful Soup会搜索每个tag的”href”属性:</p><p style="color:rgb(34,255);">soup.find_all(href=re.compile("elsie"))</p><p style="color:rgb(34,255);"># [<a class="sister" href="http://example.com/elsie" id="link1">Elsie]</p><p style="color:rgb(34,255);">搜索指定名字的属性时可以使用的参数值包括 字符串,True .</p><p style="color:rgb(34,255);">下面的例子在文档树中查找所有包含 id 属性的tag,无论 id 的值是什么:</p><p style="color:rgb(34,255);">soup.find_all(id=True)</p><p style="color:rgb(34,255);">使用多个指定名字的参数可以同时过滤tag的多个属性:</p><p style="color:rgb(34,255);">soup.find_all(href=re.compile("elsie"),id='link1')</p><p style="color:rgb(34,255);"># [<a class="sister" href="http://example.com/elsie" id="link1">three]</p><p style="color:rgb(34,255);">有些tag属性在搜索不能使用,比如HTML5中的 data-</em> 属性:</p><p style="color:rgb(34,255);">data_soup = BeautifulSoup('<div data-foo="value">foo!</div>')</p><p style="color:rgb(34,255);">data_soup.find_all(data-foo="value")</p><p style="color:rgb(34,255);"># SyntaxError: keyword can't be an expression</p><p style="color:rgb(34,255);">但是可以通过 find_all() 方法的 attrs 参数定义一个字典参数来搜索包含特殊属性的tag:</p><p style="color:rgb(34,255);">data_soup.find<em>all(attrs={"data-foo": "value"})</p><p style="color:rgb(34,255);"># [<div data-foo="value">foo!</div>]</p><p style="color:rgb(34,255);">按CSS搜索</p><p style="color:rgb(34,255);">按照CSS类名搜索tag的功能非常实用,但标识CSS类名的关键字 class 在Python中是保留字,使用 class 做参数会导致语法错误.从Beautiful Soup的4.1.1版本开始,可以通过 class</em> 参数搜索有指定CSS类名的tag:</p><p style="color:rgb(34,255);">soup.find<em>all("a",class</em>="sister")</p><p style="color:rgb(34,255);">class_ 参数同样接受不同类型的 过滤器,字符串,方法或 True :</p><p style="color:rgb(34,255);">soup.find<em>all(class</em>=re.compile("itl"))</p><p style="color:rgb(34,255);"><span style="font-weight:700;">def</span>?has_six_characters(css_class):</p><p style="color:rgb(34,255);"><span style="font-weight:700;">return</span>?css_class?<span style="font-weight:700;">is</span>?<span style="font-weight:700;">not</span>?None?<span style="font-weight:700;">and</span>?len(css_class) == 6</p><p style="color:rgb(34,255);">soup.find<em>all(class</em>=has_six_characters)</p><p style="color:rgb(34,255);">tag的 class 属性是 多值属性 .按照CSS类名搜索tag时,可以分别搜索tag中的每个CSS类名:</p><p style="color:rgb(34,255);">css_soup.find<em>all("p",class</em>="strikeout")</p><p style="color:rgb(34,255);"># [<p class="body strikeout"></p>]</p><p style="color:rgb(34,class<em>="body")</p><p style="color:rgb(34,255);">搜索 class 属性时也可以通过CSS值完全匹配:</p><p style="color:rgb(34,class</em>="body strikeout")</p><p style="color:rgb(34,255);">完全匹配 class 的值时,如果CSS类名的顺序与实际不符,将搜索不到结果:</p><p style="color:rgb(34,attrs={"class": "sister"})</p><p style="color:rgb(34,255);">text 参数</p><p style="color:rgb(34,255);">通过 text 参数可以搜搜文档中的字符串内容.与 name 参数的可选值一样,text 参数接受 字符串,True . 看例子:</p><p style="color:rgb(34,255);">soup.find_all(text="Elsie")</p><p style="color:rgb(34,255);"># [u'Elsie']</p><p style="color:rgb(34,255);">soup.find_all(text=["Tillie","Elsie","Lacie"])</p><p style="color:rgb(34,255);"># [u'Elsie',u'Lacie',u'Tillie']</p><p style="color:rgb(34,255);">soup.find_all(text=re.compile("Dormouse"))</p><p style="color:rgb(34,255);">[u"The Dormouse's story",u"The Dormouse's story"]</p><p style="color:rgb(34,255);">def is_the_only_string_within_a_tag(s):</p><p style="color:rgb(34,255);">""Return True if this string is the only child of its parent tag.""</p><p style="color:rgb(34,255);">return (s == s.parent.string)</p><p style="color:rgb(34,255);">soup.find_all(text=is_the_only_string_within_a_tag)</p><p style="color:rgb(34,255);"># [u"The Dormouse's story",u"The Dormouse's story",u'Elsie',u'Tillie',u'...']</p><p style="color:rgb(34,255);">虽然 text 参数用于搜索字符串,还可以与其它参数混合使用来过滤tag.Beautiful Soup会找到 .string 方法与 text 参数值相符的tag.下面代码用来搜索内容里面包含“Elsie”的标签:</p><p style="color:rgb(34,text="Elsie")</p><p style="color:rgb(34,255);"># [<a href="http://example.com/elsie" class="sister" id="link1">Elsie]</p><p style="color:rgb(34,255);">limit 参数</p><p style="color:rgb(34,255);">find_all() 方法返回全部的搜索结构,如果文档树很大那么搜索会很慢.如果我们不需要全部结果,可以使用 limit 参数限制返回结果的数量.效果与SQL中的limit关键字类似,当搜索到的结果数量达到 limit 的限制时,就停止搜索返回结果.</p><p style="color:rgb(34,255);">文档树中有3个tag符合搜索条件,但结果只返回了2个,因为我们限制了返回数量:</p><p style="color:rgb(34,limit=2)</p><p style="color:rgb(34,255);"># <a class="sister" href="http://example.com/lacie" id="link2">Lacie]</p><p style="color:rgb(34,255);">recursive 参数</p><p style="color:rgb(34,255);">调用tag的 find_all() 方法时,Beautiful Soup会检索当前tag的所有子孙节点,如果只想搜索tag的直接子节点,可以使用参数 recursive=False .</p><p style="color:rgb(34,255);">一段简单的文档:</p><p style="color:rgb(34,255);"><html></p><p style="color:rgb(34,255);"><head></p><p style="color:rgb(34,255);"><title></p><p style="color:rgb(34,255);">The Dormouse's story</p><p style="color:rgb(34,255);">

<p style="color:rgb(34,255);">

<p style="color:rgb(34,255);">...

<p style="color:rgb(34,255);">是否使用 recursive 参数的搜索结果:

<p style="color:rgb(34,255);">soup.html.find_all("title")

<p style="color:rgb(34,255);">soup.html.find_all("title",recursive=False)

<p style="color:rgb(34,255);"># []

<p style="color:rgb(34,255);">像调用 find_all() 一样调用tag

<p style="color:rgb(34,255);">find_all() 几乎是Beautiful Soup中最常用的搜索方法,所以我们定义了它的简写方法. BeautifulSoup 对象和 tag 对象可以被当作一个方法来使用,这个方法的执行结果与调用这个对象的 find_all() 方法相同,下面两行代码是等价的:

<p style="color:rgb(34,255);">soup("a")

<p style="color:rgb(34,255);">这两行代码也是等价的:

<p style="color:rgb(34,255);">soup.title.find_all(text=True)

<p style="color:rgb(34,255);">soup.title(text=True)

<p style="color:rgb(34,255);">find()

<p style="color:rgb(34,255);">find( name,255);">find_all() 方法将返回文档中符合条件的所有tag,尽管有时候我们只想得到一个结果.比如文档中只有一个标签,那么使用 find_all() 方法来查找标签就不太合适,使用 find_all 方法并设置 limit=1 参数不如直接使用 find() 方法.下面两行代码是等价的:

<p style="color:rgb(34,255);">soup.find_all('title',limit=1)

<p style="color:rgb(34,255);">soup.find('title')

<p style="color:rgb(34,255);">唯一的区别是 find_all() 方法的返回结果是值包含一个元素的列表,而 find() 方法直接返回结果.

<p style="color:rgb(34,255);">find_all() 方法没有找到目标是返回空列表,find() 方法找不到目标时,返回 None .

<p style="color:rgb(34,255);"><span style="font-weight:700;">print(soup.find("nosuchtag"))

<p style="color:rgb(34,255);">soup.head.title 是 tag的名字 方法的简写.这个简写的原理就是多次调用当前tag的 find() 方法:

<p style="color:rgb(34,255);">soup.head.title

<p style="color:rgb(34,255);">soup.find("head").find("title")

<p style="color:rgb(34,255);">find_parents() 和 find_parent()

<p style="color:rgb(34,255);">find_parents( name,255);">find_parent( name,255);">我们已经用了很大篇幅来介绍 find_all() 和 find() 方法,Beautiful Soup中还有10个用于搜索的API.它们中的五个用的是与 find_all() 相同的搜索参数,另外5个与 find() 方法的搜索参数类似.区别仅是它们搜索文档的不同部分.

<p style="color:rgb(34,255);">记住: find_all() 和 find() 只搜索当前节点的所有子节点,孙子节点等. find_parents() 和 find_parent() 用来搜索当前节点的父辈节点,搜索方法与普通tag的搜索方法相同,搜索文档搜索文档包含的内容. 我们从一个文档中的一个叶子节点开始:

<p style="color:rgb(34,255);">a_string = soup.find(text="Lacie")

<p style="color:rgb(34,255);">a_string

<p style="color:rgb(34,255);">a_string.find_parents("a")

<p style="color:rgb(34,255);">a_string.find_parent("p")

<p style="color:rgb(34,255);"># <p class="story">Once upon a time there were three little sisters; and their names were

<p style="color:rgb(34,255);"># <a class="sister" href="http://example.com/lacie" id="link2">Lacie and

<p style="color:rgb(34,255);"># <a class="sister" href="http://example.com/tillie" id="link3">Tillie;

<p style="color:rgb(34,255);"># and they lived at the bottom of a well.

<p style="color:rgb(34,255);">a_string.find_parents("p",class="title")

<p style="color:rgb(34,255);">文档中的一个标签是是当前叶子节点的直接父节点,所以可以被找到.还有一个

标签,是目标叶子节点的间接父辈节点,所以也可以被找到.包含class值为”title”的

标签不是不是目标叶子节点的父辈节点,所以通过 find_parents() 方法搜索不到.

<p style="color:rgb(34,255);">find_parent() 和 find_parents() 方法会让人联想到 .parent 和 .parents 属性.它们之间的联系非常紧密.搜索父辈节点的方法实际上就是对 .parents 属性的迭代搜索.

<p style="color:rgb(34,255);">find_next_siblings() 合 find_next_sibling()

<p style="color:rgb(34,255);">find_next_siblings( name,255);">find_next_sibling( name,255);">这2个方法通过 .next_siblings 属性对当tag的所有后面解析 [5] 的兄弟tag节点进行迭代,find_next_siblings() 方法返回所有符合条件的后面的兄弟节点,find_next_sibling() 只返回符合条件的后面的第一个tag节点.

<p style="color:rgb(34,255);">first_link = soup.a

<p style="color:rgb(34,255);">first_link

<p style="color:rgb(34,255);">first_link.find_next_siblings("a")

<p style="color:rgb(34,255);"># [<a class="sister" href="http://example.com/lacie" id="link2">Lacie,255);">first_story_paragraph = soup.find("p","story")

<p style="color:rgb(34,255);">first_story_paragraph.find_next_sibling("p")

<p style="color:rgb(34,255);">find_previous_siblings() 和 find_previous_sibling()

<p style="color:rgb(34,255);">find_previous_siblings( name,255);">find_previous_sibling( name,255);">这2个方法通过 .previous_siblings 属性对当前tag的前面解析 [5] 的兄弟tag节点进行迭代,find_previous_siblings() 方法返回所有符合条件的前面的兄弟节点,find_previous_sibling() 方法返回第一个符合条件的前面的兄弟节点:

<p style="color:rgb(34,255);">last_link = soup.find("a",255);">last_link

<p style="color:rgb(34,255);">last_link.find_previous_siblings("a")

<p style="color:rgb(34,255);"># <a class="sister" href="http://example.com/elsie" id="link1">Elsie]

<p style="color:rgb(34,255);">first_story_paragraph.find_previous_sibling("p")

<p style="color:rgb(34,255);">find_all_next() 和 find_next()

<p style="color:rgb(34,255);">find_all_next( name,255);">find_next( name,255);">这2个方法通过 .next_elements 属性对当前tag的之后的 [5] tag和字符串进行迭代,find_all_next() 方法返回所有符合条件的节点,find_next() 方法返回第一个符合条件的节点:

<p style="color:rgb(34,255);">first_link.find_all_next(text=True)

<p style="color:rgb(34,u',',u' and ',255);"># u'; and they lived at the bottom of a well.',u' ',u'...',u' ']

<p style="color:rgb(34,255);">first_link.find_next("p")

<p style="color:rgb(34,255);">第一个例子中,字符串 “Elsie”也被显示出来,尽管它被包含在我们开始查找的标签的里面.第二个例子中,最后一个

标签也被显示出来,尽管它与我们开始查找位置的标签不属于同一部分.例子中,搜索的重点是要匹配过滤器的条件,并且在文档中出现的顺序而不是开始查找的元素的位置.

<p style="color:rgb(34,255);">find_all_previous() 和 find_previous()

<p style="color:rgb(34,255);">find_all_previous( name,255);">find_previous( name,255);">这2个方法通过 .previous_elements 属性对当前节点前面 [5] 的tag和字符串进行迭代,find_all_previous() 方法返回所有符合条件的节点,find_previous() 方法返回第一个符合条件的节点.

<p style="color:rgb(34,255);">first_link.find_all_previous("p")

<p style="color:rgb(34,255);"># [<p class="story">Once upon a time there were three little sisters; ...

,255);"># <p class="title">The Dormouse's story

]

<p style="color:rgb(34,255);">first_link.find_previous("title")

<p style="color:rgb(34,255);">find_all_previous("p") 返回了文档中的第一段(class=”title”的那段),但还返回了第二段,

标签包含了我们开始查找的标签.不要惊讶,这段代码的功能是查找所有出现在指定标签之前的

标签,因为这个

标签包含了开始的标签,所以

标签一定是在之前出现的.

<p style="color:rgb(34,255);">CSS选择器

<p style="color:rgb(34,255);">Beautiful Soup支持大部分的CSS选择器 [6],在 Tag 或 BeautifulSoup 对象的 .select() 方法中传入字符串参数,即可使用CSS选择器的语法找到tag:

<p style="color:rgb(34,255);">soup.select("title")

<p style="color:rgb(34,255);">soup.select("p nth-of-type(3)")

<p style="color:rgb(34,255);"># [<p class="story">...

]

<p style="color:rgb(34,255);">通过tag标签逐层查找:

<p style="color:rgb(34,255);">soup.select("body a")

<p style="color:rgb(34,255);">soup.select("html head title")

<p style="color:rgb(34,255);">找到某个tag标签下的直接子标签 [6] :

<p style="color:rgb(34,255);">soup.select("head > title")

<p style="color:rgb(34,255);">soup.select("p > a")

<p style="color:rgb(34,255);">soup.select("p > a:nth-of-type(2)")

<p style="color:rgb(34,255);">soup.select("p > #link1")

<p style="color:rgb(34,255);">soup.select("body > a")

<p style="color:rgb(34,255);">找到兄弟节点标签:

<p style="color:rgb(34,255);">soup.select("#link1 ~ .sister")

<p style="color:rgb(34,255);">soup.select("#link1 + .sister")

<p style="color:rgb(34,255);">通过CSS的类名查找:

<p style="color:rgb(34,255);">soup.select(".sister")

<p style="color:rgb(34,255);">soup.select("[class~=sister]")

<p style="color:rgb(34,255);">通过tag的id查找:

<p style="color:rgb(34,255);">soup.select("#link1")

<p style="color:rgb(34,255);">soup.select("a#link2")

<p style="color:rgb(34,255);">通过是否存在某个属性来查找:

<p style="color:rgb(34,255);">soup.select('a[href]')

<p style="color:rgb(34,255);">通过属性的值来查找:

<p style="color:rgb(34,255);">soup.select('a[href="http://example.com/elsie"]')

<p style="color:rgb(34,255);">soup.select('a[href^="http://example.com/"]')

<p style="color:rgb(34,255);">soup.select('a[href$="tillie"]')

<p style="color:rgb(34,255);"># [<a class="sister" href="http://example.com/tillie" id="link3">Tillie]

<p style="color:rgb(34,255);">soup.select('a[href*=".com/el"]')

<p style="color:rgb(34,255);">通过语言设置来查找:

<p style="color:rgb(34,255);">multilingual_markup = """

<p style="color:rgb(34,255);"><p lang="en">Hello

<p style="color:rgb(34,255);"><p lang="en-us">Howdy,y'all

<p style="color:rgb(34,255);"><p lang="en-gb">Pip-pip,old fruit

<p style="color:rgb(34,255);"><p lang="fr">Bonjour mes amis

<p style="color:rgb(34,255);">multilingual_soup = BeautifulSoup(multilingual_markup)

<p style="color:rgb(34,255);">multilingual_soup.select('p[lang|=en]')

<p style="color:rgb(34,255);"># [<p lang="en">Hello

,255);"># <p lang="en-us">Howdy,y'all

,255);"># <p lang="en-gb">Pip-pip,old fruit

]

<p style="color:rgb(34,255);">对于熟悉CSS选择器语法的人来说这是个非常方便的方法.Beautiful Soup也支持CSS选择器API,如果你仅仅需要CSS选择器的功能,那么直接使用 lxml 也可以,而且速度更快,支持更多的CSS选择器语法,但Beautiful Soup整合了CSS选择器的语法和自身方便使用API.

<h1 style="font-size:24px;line-height:32px;color:rgb(34,255);">修改文档树<p style="color:rgb(34,255);">Beautiful Soup的强项是文档树的搜索,但同时也可以方便的修改文档树

<p style="color:rgb(34,255);">修改tag的名称和属性

<p style="color:rgb(34,255);">在 Attributes 的章节中已经介绍过这个功能,但是再看一遍也无妨. 重命名一个tag,改变属性的值,添加或删除属性:

<p style="color:rgb(34,255);">修改 .string

<p style="color:rgb(34,255);">给tag的 .string 属性赋值,就相当于用当前的内容替代了原来的内容:

<p style="color:rgb(34,255);">markup = '<a href="http://example.com/"&gt;I linked to example.com'

<p style="color:rgb(34,255);">tag = soup.a

<p style="color:rgb(34,255);">tag.string = "New link text."

<p style="color:rgb(34,255);"># <a href="http://example.com/"&gt;New link text.

<p style="color:rgb(34,255);">注意: 如果当前的tag包含了其它tag,那么给它的 .string 属性赋值会覆盖掉原有的所有内容包括子tag

<p style="color:rgb(34,255);">append()

<p style="color:rgb(34,255);">Tag.append() 方法想tag中添加内容,就好像Python的列表的 .append() 方法:

<p style="color:rgb(34,255);">soup = BeautifulSoup("Foo")

<p style="color:rgb(34,255);">soup.a.append("Bar")

<p style="color:rgb(34,255);">soup

<p style="color:rgb(34,255);"># FooBar

<p style="color:rgb(34,255);">soup.a.contents

<p style="color:rgb(34,255);"># [u'Foo',u'Bar']

<p style="color:rgb(34,255);">BeautifulSoup.new_string() 和 .new_tag()

<p style="color:rgb(34,255);">如果想添加一段文本内容到文档中也没问题,可以调用Python的 append() 方法或调用工厂方法 BeautifulSoup.new_string() :

<p style="color:rgb(34,255);">soup = BeautifulSoup("")

<p style="color:rgb(34,255);">tag.append("Hello")

<p style="color:rgb(34,255);">new_string = soup.new_string(" there")

<p style="color:rgb(34,255);">tag.append(new_string)

<p style="color:rgb(34,255);"># Hello there.

<p style="color:rgb(34,255);">tag.contents

<p style="color:rgb(34,255);"># [u'Hello',u' there']

<p style="color:rgb(34,255);">如果想要创建一段注释,或 NavigableString 的任何子类,将子类作为 new_string() 方法的第二个参数传入:

<p style="color:rgb(34,255);"><span style="font-weight:700;">from?<span style="font-weight:700;">bs4?<span style="font-weight:700;">import?Comment

<p style="color:rgb(34,255);">new_comment = soup.new_string("Nice to see you.",Comment)

<p style="color:rgb(34,255);">tag.append(new_comment)

<p style="color:rgb(34,255);"># Hello there

<p style="color:rgb(34,u' there',u'Nice to see you.']

<p style="color:rgb(34,255);"># 这是Beautiful Soup 4.2.1 中新增的方法

<p style="color:rgb(34,255);">创建一个tag最好的方法是调用工厂方法 BeautifulSoup.new_tag() :

<p style="color:rgb(34,255);">original_tag = soup.b

<p style="color:rgb(34,255);">new_tag = soup.new_tag("a",href="http://www.example.com")

<p style="color:rgb(34,255);">original_tag.append(new_tag)

<p style="color:rgb(34,255);">original_tag

<p style="color:rgb(34,255);"># <a href="http://www.example.com"&gt;

<p style="color:rgb(34,255);">new_tag.string = "Link text."

<p style="color:rgb(34,255);"># <a href="http://www.example.com"&gt;Link text.

<p style="color:rgb(34,255);">第一个参数作为tag的name,是必填,其它参数选填

<p style="color:rgb(34,255);">insert()

<p style="color:rgb(34,255);">Tag.insert() 方法与 Tag.append() 方法类似,区别是不会把新元素添加到父节点 .contents 属性的最后,而是把元素插入到指定的位置.与Python列表总的 .insert() 方法的用法下同:

<p style="color:rgb(34,255);">tag.insert(1,"but did not endorse ")

<p style="color:rgb(34,255);"># <a href="http://example.com/"&gt;I linked to but did not endorse example.com

<p style="color:rgb(34,255);"># [u'I linked to ',u'but did not endorse',example.com]

<p style="color:rgb(34,255);">insert_before() 和 insert_after()

<p style="color:rgb(34,255);">insert_before() 方法在当前tag或文本节点前插入内容:

<p style="color:rgb(34,255);">soup = BeautifulSoup("stop")

<p style="color:rgb(34,255);">tag = soup.new_tag("i")

<p style="color:rgb(34,255);">tag.string = "Don't"

<p style="color:rgb(34,255);">soup.b.string.insert_before(tag)

<p style="color:rgb(34,255);">soup.b

<p style="color:rgb(34,255);"># Don'tstop

<p style="color:rgb(34,255);">insert_after() 方法在当前tag或文本节点后插入内容:

<p style="color:rgb(34,255);">soup.b.i.insert_after(soup.new_string(" ever "))

<p style="color:rgb(34,255);"># Don't ever stop

<p style="color:rgb(34,255);">soup.b.contents

<p style="color:rgb(34,255);"># [Don't,u' ever ',u'stop']

<p style="color:rgb(34,255);">clear()

<p style="color:rgb(34,255);">Tag.clear() 方法移除当前tag的内容:

<p style="color:rgb(34,255);">tag.clear()

<p style="color:rgb(34,255);"># <a href="http://example.com/"&gt;

<p style="color:rgb(34,255);">extract()

<p style="color:rgb(34,255);">PageElement.extract() 方法将当前tag移除文档树,并作为方法结果返回:

<p style="color:rgb(34,255);">a_tag = soup.a

<p style="color:rgb(34,255);">i_tag = soup.i.extract()

<p style="color:rgb(34,255);">a_tag

<p style="color:rgb(34,255);"># <a href="http://example.com/"&gt;I linked to

<p style="color:rgb(34,255);">i_tag

<p style="color:rgb(34,255);"># example.com

<p style="color:rgb(34,255);"><span style="font-weight:700;">print(i_tag.parent)

<p style="color:rgb(34,255);">None

<p style="color:rgb(34,255);">这个方法实际上产生了2个文档树: 一个是用来解析原始文档的 BeautifulSoup 对象,另一个是被移除并且返回的tag.被移除并返回的tag可以继续调用 extract 方法:

<p style="color:rgb(34,255);">my_string = i_tag.string.extract()

<p style="color:rgb(34,255);">my_string

<p style="color:rgb(34,255);"># u'example.com'

<p style="color:rgb(34,255);"><span style="font-weight:700;">print(my_string.parent)

<p style="color:rgb(34,255);">#

<p style="color:rgb(34,255);">decompose()

<p style="color:rgb(34,255);">Tag.decompose() 方法将当前节点移除文档树并完全销毁:

<p style="color:rgb(34,255);">soup.i.decompose()

<p style="color:rgb(34,255);">replace_with()

<p style="color:rgb(34,255);">PageElement.replace_with() 方法移除文档树中的某段内容,并用新tag或文本节点替代它:

<p style="color:rgb(34,255);">new_tag = soup.new_tag("b")

<p style="color:rgb(34,255);">new_tag.string = "example.net"

<p style="color:rgb(34,255);">a_tag.i.replace_with(new_tag)

<p style="color:rgb(34,255);"># <a href="http://example.com/"&gt;I linked to example.net

<p style="color:rgb(34,255);">replace_with() 方法返回被替代的tag或文本节点,可以用来浏览或添加到文档树其它地方

<p style="color:rgb(34,255);">wrap()

<p style="color:rgb(34,255);">PageElement.wrap() 方法可以对指定的tag元素进行包装 [8],并返回包装后的结果:

<p style="color:rgb(34,255);">soup = BeautifulSoup("

I wish I was bold.

")

<p style="color:rgb(34,255);">soup.p.string.wrap(soup.new_tag("b"))

<p style="color:rgb(34,255);"># I wish I was bold.

<p style="color:rgb(34,255);">soup.p.wrap(soup.new_tag("div"))

<p style="color:rgb(34,255);">#

I wish I was bold.

<p style="color:rgb(34,255);">该方法在 Beautiful Soup 4.0.5 中添加

<p style="color:rgb(34,255);">unwrap()

<p style="color:rgb(34,255);">Tag.unwrap() 方法与 wrap() 方法相反.将移除tag内的所有tag标签,该方法常被用来进行标记的解包:

<p style="color:rgb(34,255);">a_tag.i.unwrap()

<p style="color:rgb(34,255);"># <a href="http://example.com/"&gt;I linked to example.com

<p style="color:rgb(34,255);">与 replace_with() 方法相同,unwrap() 方法返回被移除的tag

<h1 style="font-size:24px;line-height:32px;color:rgb(34,255);">输出<p style="color:rgb(34,255);">格式化输出

<p style="color:rgb(34,255);">prettify() 方法将Beautiful Soup的文档树格式化后以Unicode编码输出,每个XML/HTML标签都独占一行

<p style="color:rgb(34,255);">soup.prettify()

<p style="color:rgb(34,255);"># ' <a href="http://example.com/"&gt; ...'

<p style="color:rgb(34,255);"># <a href="http://example.com/"&gt;

<p style="color:rgb(34,255);"># I linked to

<p style="color:rgb(34,255);">#

<p style="color:rgb(34,255);"># example.com

<p style="color:rgb(34,255);">#

<p style="color:rgb(34,255);">BeautifulSoup 对象和它的tag节点都可以调用 prettify() 方法:

<p style="color:rgb(34,255);"><span style="font-weight:700;">print(soup.a.prettify())

<p style="color:rgb(34,255);">压缩输出

<p style="color:rgb(34,255);">如果只想得到结果字符串,不重视格式,那么可以对一个 BeautifulSoup 对象或 Tag 对象使用Python的 unicode() 或 str() 方法:

<p style="color:rgb(34,255);">str(soup)

<p style="color:rgb(34,255);"># '<a href="http://example.com/"&gt;I linked to example.com'

<p style="color:rgb(34,255);">unicode(soup.a)

<p style="color:rgb(34,255);"># u'<a href="http://example.com/"&gt;I linked to example.com'

<p style="color:rgb(34,255);">str() 方法返回UTF-8编码的字符串,可以指定 编码 的设置.

<p style="color:rgb(34,255);">还可以调用 encode() 方法获得字节码或调用 decode() 方法获得Unicode.

<p style="color:rgb(34,255);">输出格式

<p style="color:rgb(34,255);">Beautiful Soup输出是会将HTML中的特殊字符转换成Unicode,比如“&lquot;”:

<p style="color:rgb(34,255);">soup = BeautifulSoup("“Dammit!” he said.")

<p style="color:rgb(34,255);">unicode(soup)

<p style="color:rgb(34,255);"># u'u201cDammit!u201d he said.'

<p style="color:rgb(34,255);">如果将文档转换成字符串,Unicode编码会被编码成UTF-8.这样就无法正确显示HTML特殊字符了:

<p style="color:rgb(34,255);"># 'xe2x80x9cDammit!xe2x80x9d he said.'

<p style="color:rgb(34,255);">get_text()

<p style="color:rgb(34,255);">如果只想得到tag中包含的文本内容,那么可以嗲用 get_text() 方法,这个方法获取到tag中包含的所有文版内容包括子孙tag中的内容,并将结果作为Unicode字符串返回:

<p style="color:rgb(34,255);">markup = '<a href="http://example.com/"&gt;<span style="font-weight:700;">?I linked to example.com<span style="font-weight:700;">?'

<p style="color:rgb(34,255);">soup.get_text()

<p style="color:rgb(34,255);">u'<span style="font-weight:700;">?I linked to example.com<span style="font-weight:700;">?'

<p style="color:rgb(34,255);">soup.i.get_text()

<p style="color:rgb(34,255);">u'example.com'

<p style="color:rgb(34,255);">可以通过参数指定tag的文本内容的分隔符:

<p style="color:rgb(34,255);"># soup.get_text("|")

<p style="color:rgb(34,255);">u'<span style="font-weight:700;">?I linked to |example.com|<span style="font-weight:700;">?'

<p style="color:rgb(34,255);">还可以去除获得文本内容的前后空白:

<p style="color:rgb(34,255);"># soup.get_text("|",strip=True)

<p style="color:rgb(34,255);">u'I linked to|example.com'

<p style="color:rgb(34,255);">或者使用 .stripped_strings 生成器,获得文本列表后手动处理列表:

<p style="color:rgb(34,255);">[text?<span style="font-weight:700;">for?text?<span style="font-weight:700;">in?soup.stripped_strings]

<p style="color:rgb(34,255);"># [u'I linked to',u'example.com']

<h1 style="font-size:24px;line-height:32px;color:rgb(34,255);">指定文档解析器<p style="color:rgb(34,255);">如果仅是想要解析HTML文档,只要用文档创建 BeautifulSoup 对象就可以了.Beautiful Soup会自动选择一个解析器来解析文档.但是还可以通过参数指定使用那种解析器来解析当前文档.

<p style="color:rgb(34,255);">BeautifulSoup 第一个参数应该是要被解析的文档字符串或是文件句柄,第二个参数用来标识怎样解析文档.如果第二个参数为空,那么Beautiful Soup根据当前系统安装的库自动选择解析器,解析器的优先数序: lxml,html5lib,Python标准库.在下面两种条件下解析器优先顺序会变化:

<ul style="list-style:square outside;color:rgb(34,255);"><li style="margin-left:0px;list-style:inherit;">要解析的文档是什么类型: 目前支持,“html”,“xml”,和 “html5”<li style="margin-left:0px;list-style:inherit;">指定使用哪种解析器: 目前支持,“lxml”,“html5lib”,和 “html.parser”<p style="color:rgb(34,255);">安装解析器 章节介绍了可以使用哪种解析器,以及如何安装.

<p style="color:rgb(34,255);">如果指定的解析器没有安装,Beautiful Soup会自动选择其它方案.目前只有 lxml 解析器支持XML文档的解析,在没有安装lxml库的情况下,创建 beautifulsoup 对象时无论是否指定使用lxml,都无法得到解析后的对象

<p style="color:rgb(34,255);">解析器之间的区别

<p style="color:rgb(34,255);">Beautiful Soup为不同的解析器提供了相同的接口,但解析器本身时有区别的.同一篇文档被不同的解析器解析后可能会生成不同结构的树型文档.区别最大的是HTML解析器和XML解析器,看下面片段被解析成HTML结构:

<p style="color:rgb(34,255);">BeautifulSoup("")

<p style="color:rgb(34,255);">#

<p style="color:rgb(34,255);">因为空标签不符合HTML标准,所以解析器把它解析成

<p style="color:rgb(34,255);">同样的文档使用XML解析如下(解析XML需要安装lxml库).注意,空标签依然被保留,并且文档前添加了XML头,而不是被包含在标签内:

<p style="color:rgb(34,255);">BeautifulSoup("","xml")

<p style="color:rgb(34,255);"># <?xml version="1.0" encoding="utf-8"?>

<p style="color:rgb(34,255);">#

<p style="color:rgb(34,255);">HTML解析器之间也有区别,如果被解析的HTML文档是标准格式,那么解析器之间没有任何差别,只是解析速度不同,结果都会返回正确的文档树.

<p style="color:rgb(34,255);">但是如果被解析文档不是标准格式,那么不同的解析器返回结果可能不同.下面例子中,使用lxml解析错误格式的文档,结果

标签被直接忽略掉了:

<p style="color:rgb(34,255);">BeautifulSoup("

","lxml")

<p style="color:rgb(34,255);">#

<p style="color:rgb(34,255);">使用html5lib库解析相同文档会得到不同的结果:

<p style="color:rgb(34,"html5lib")

<p style="color:rgb(34,255);">#

<p style="color:rgb(34,255);">html5lib库没有忽略掉

标签,而是自动补全了标签,还给文档树添加了标签.

<p style="color:rgb(34,255);">使用pyhton内置库解析结果如下:

<p style="color:rgb(34,"html.parser")

<p style="color:rgb(34,255);">#

<p style="color:rgb(34,255);">与lxml [7] 库类似的,Python内置库忽略掉了

标签,与html5lib库不同的是标准库没有尝试创建符合标准的文档格式或将文档片段包含在标签内,与lxml不同的是标准库甚至连标签都没有尝试去添加.

<p style="color:rgb(34,255);">因为文档片段“

”是错误格式,所以以上解析方式都能算作”正确”,html5lib库使用的是HTML5的部分标准,所以最接近”正确”.不过所有解析器的结构都能够被认为是”正常”的.

<p style="color:rgb(34,255);">不同的解析器可能影响代码执行结果,如果在分发给别人的代码中使用了 BeautifulSoup,那么最好注明使用了哪种解析器,以减少不必要的麻烦.

<h1 style="font-size:24px;line-height:32px;color:rgb(34,255);">编码<p style="color:rgb(34,255);">任何HTML或XML文档都有自己的编码方式,比如ASCII 或 UTF-8,但是使用Beautiful Soup解析后,文档都被转换成了Unicode:

<p style="color:rgb(34,255);">markup = "

Sacr<span style="font-weight:700;">xc3xa9?bleu!

"

<p style="color:rgb(34,255);">soup.h1

<p style="color:rgb(34,255);">#

Sacré bleu!

<p style="color:rgb(34,255);">soup.h1.string

<p style="color:rgb(34,255);"># u'Sacrxe9 bleu!'

<p style="color:rgb(34,255);">这不是魔术(但很神奇),Beautiful Soup用了 编码自动检测 子库来识别当前文档编码并转换成Unicode编码. BeautifulSoup 对象的 .original_encoding 属性记录了自动识别编码的结果:

<p style="color:rgb(34,255);">soup.original_encoding

<p style="color:rgb(34,255);">'utf-8'

<p style="color:rgb(34,255);">编码自动检测 功能大部分时候都能猜对编码格式,但有时候也会出错.有时候即使猜测正确,也是在逐个字节的遍历整个文档后才猜对的,这样很慢.如果预先知道文档编码,可以设置编码参数来减少自动检查编码出错的概率并且提高文档解析速度.在创建 BeautifulSoup 对象的时候设置 from_encoding 参数.

<p style="color:rgb(34,255);"><span style="font-weight:700;">私信小编007即可获取数十套PDF!


(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读