没有合适的资源?快使用搜索试试~ 我知道了~
首页Python模块 - Beautifulsoup中文手册
Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你节省数小时甚至数天的工作时间. 这篇文档介绍了BeautifulSoup4中所有主要特性,并且有小例子.让我来向你展示它适合做什么,如何工作,怎样使用,如何达到你想要的效果,和处理异常情况. 文档中出现的例子在Python2.7和Python3.2中的执行结果相同 你可能在寻找 Beautiful Soup3 的文档,Beautiful Soup 3 目前已经停止开发,我们推荐在现在的项目中使用Beautiful Soup 4, 移植到BS4
资源详情
资源评论
资源推荐

Beautiful Soup 4.2.0
文
档
Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文
档导航,查找,修改文档的方式.Beautiful Soup会帮你节省数小时甚至数天的工作时间.
这篇文档介绍了BeautifulSoup4中所有主要特性,并且有小例子.让我来向你展示它适合做什么,如何工作,怎样使用,
如何达到你想要的效果,和处理异常情况.
文档中出现的例子在Python2.7和Python3.2中的执行结果相同
你可能在寻找 Beautiful Soup3 的文档,Beautiful Soup 3 目前已经停止开发,我们推荐在现在的项目中使用
Beautiful Soup 4, 移植到BS4
寻
求
帮
助
如果你有关于BeautifulSoup的问题,可以发送邮件到 讨论组 .如果你的问题包含了一段需要转换的HTML代码,那么
确保你提的问题描述中附带这段HTML文档的 代码诊断 [1]
快
速
开
始
下面的一段HTML代码将作为例子被多次用到.这是
爱
丽丝
梦
游
仙
境
的
的一段内容(以后内容中简称为
爱
丽丝
的文
档):
使用BeautifulSoup解析这段代码,能够得到一个 BeautifulSoup 的对象,并能按照标准的缩进格式的结构输出:
html_doc="""
<html><head><title>TheDormouse'sstory</title></head>
<body>
<pclass="title"><b>TheDormouse'sstory</b></p>
<pclass="story">Onceuponatimetherewerethreelittlesisters;andtheirnameswere
<ahref="http://example.com/elsie"class="sister"id="link1">Elsie</a>,
<ahref="http://example.com/lacie"class="sister"id="link2">Lacie</a>and
<ahref="http://example.com/tillie"class="sister"id="link3">Tillie</a>;
andtheylivedatthebottomofawell.</p>
<pclass="story">...</p>
"""

几个简单的浏览结构化数据的方法:
frombs4importBeautifulSoup
soup=BeautifulSoup(html_doc)
print(soup.prettify())
#<html>
#<head>
#<title>
#TheDormouse'sstory
#</title>
#</head>
#<body>
#<pclass="title">
#<b>
#TheDormouse'sstory
#</b>
#</p>
#<pclass="story">
#Onceuponatimetherewerethreelittlesisters;andtheirnameswere
#<aclass="sister"href="http://example.com/elsie"id="link1">
#Elsie
#</a>
#,
#<aclass="sister"href="http://example.com/lacie"id="link2">
#Lacie
#</a>
#and
#<aclass="sister"href="http://example.com/tillie"id="link2">
#Tillie
#</a>
#;andtheylivedatthebottomofawell.
#</p>
#<pclass="story">
#...
#</p>
#</body>
#</html>

从文档中找到所有 <a> 标签的链接:
从文档中获取所有文字内容:
soup.title
#<title>TheDormouse'sstory</title>
soup.title.name
#u'title'
soup.title.string
#u'TheDormouse'sstory'
soup.title.parent.name
#u'head'
soup.p
#<pclass="title"><b>TheDormouse'sstory</b></p>
soup.p['class']
#u'title'
soup.a
#<aclass="sister"href="http://example.com/elsie"id="link1">Elsie</a>
soup.find_all('a')
#[<aclass="sister"href="http://example.com/elsie"id="link1">Elsie</a>,
#<aclass="sister"href="http://example.com/lacie"id="link2">Lacie</a>,
#<aclass="sister"href="http://example.com/tillie"id="link3">Tillie</a>]
soup.find(id="link3")
#<aclass="sister"href="http://example.com/tillie"id="link3">Tillie</a>
forlinkinsoup.find_all('a'):
print(link.get('href'))
#http://example.com/elsie
#http://example.com/lacie
#http://example.com/tillie
剩余12页未读,继续阅读


















安全验证
文档复制为VIP权益,开通VIP直接复制

评论0