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

<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;">importBeautifulSoup

<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><p style="color:rgb(34,255);"># The Dormouse's story</p> <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;">forlink<span style="font-weight:700;">insoup.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;">deltag['class']

<p style="color:rgb(34,255);"><span style="font-weight:700;">deltag['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('

dawei

【声明】:唐山站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。