登录  /  注册

python中BeautifulSoup的详细介绍(附代码)

不言
发布: 2018-12-12 09:40:46
转载
2626人浏览过

本篇文章给大家带来的内容是关于python中beautifulsoup的详细介绍(附代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

Beautiful Soup提供一些简单的、python式的函数用来处理导航、搜索、修改分析树等功能。它是一个工具箱,通过解析文档为用户提供需要抓取的数据,因为简单,所以不需要多少代码就可以写出一个完整的应用程序。Beautiful Soup自动将输入文档转换为Unicode编码,输出文档转换为utf-8编码。你不需要考虑编码方式,除非文档没有指定一个编码方式,这时,Beautiful Soup就不能自动识别编码方式了。然后,你仅仅需要说明一下原始编码方式就可以了。 Beautiful Soup已成为和lxml、html6lib一样出色的python解释器,为用户灵活地提供不同的解析策略或强劲的速度。

安装

pip install BeautifulSoup4
easy_install BeautifulSoup4
登录后复制

创建BeautifulSoup对象

首先应该导入BeautifulSoup类库 from bs4 import BeautifulSoup

下面开始创建对像,在开始之前为了方便演示,先创建一个html文本,如下:

html = """
<html><head><title>The Dormouse&#39;s story</title></head>
<body>
<p class="title" name="dromouse"><b>The Dormouse&#39;s story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1"><!-- Elsie --></a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""
登录后复制

创建对象:soup=BeautifulSoup(html,'lxml'),这里的lxml是解析的类库,目前来说个人觉得最好的解析器了,一直在用这个,安装方法:pip install lxml

Tag

Tag就是html中的一个标签,用BeautifulSoup就能解析出来Tag的具体内容,具体的格式为soup.name,其中name是html下的标签,具体实例如下:

print soup.title输出title标签下的内容,包括此标签,这个将会输出The Dormouse's story

print soup.head

注意:

这里的格式只能获取这些标签的第一个,后面会讲到获取多个标签的方法。其中对于Tag有两个重要的属性name和attrs,分别表示名字和属性,介绍如下:

name:对于Tag,它的name就是其本身,如soup.p.name就是p

attrs是一个字典类型的,对应的是属性-值,如print soup.p.attrs,输出的就是{'class': ['title'], 'name': 'dromouse'},当然你也可以得到具体的值,如print soup.p.attrs['class'],输出的就是[title]是一个列表的类型,因为一个属性可能对应多个值,当然你也可以通过get方法得到属性的,如:print soup.p.get('class')。还可以直接使用print soup.p['class']

get

get方法用于得到标签下的属性值,注意这是一个重要的方法,在许多场合都能用到,比如你要得到python中BeautifulSoup的详细介绍(附代码)标签下的图像url,那么就可以用soup.img.get('src'),具体解析如下:

print soup.p.get("class")   #得到第一个p标签下的src属性
登录后复制

string

得到标签下的文本内容,只有在此标签下没有子标签,或者只有一个子标签的情况下才能返回其中的内容,否则返回的是None具体实例如下:

print soup.p.string #在上面的一段文本中p标签没有子标签,因此能够正确返回文本的内容
print soup.html.string  #这里得到的就是None,因为这里的html中有很多的子标签
登录后复制

get_text()

可以获得一个标签中的所有文本内容,包括子孙节点的内容,这是最常用的方法

搜索文档树

find_all( name , attrs , recursive , text , **kwargs )

find_all是用于搜索节点中所有符合过滤条件的节点

1.name参数:是Tag的名字,如p,p,title …..

soup.find_all("p") 查找所有的p标签,返回的是[The Dormouse's story],可以通过遍历获取每一个节点,如下:

ps=soup.find_all("p")
for p in ps:
print p.get(&#39;class&#39;)   #得到p标签下的class属性
登录后复制

传入正则表达式:soup.find_all(re.compile(r'^b')查找以b开头的所有标签,这里的body和b标签都会被查到

传入类列表:如果传入列表参数,BeautifulSoup会将与列表中任一元素匹配的内容返回.下面代码找到文档中所有<a>标签和<b>标签

soup.find_all(["a", "b"])
登录后复制

2.KeyWords参数,就是传入属性和对应的属性值,或者一些其他的表达式

soup.find_all(id='link2'),这个将会搜索找到所有的id属性为link2的标签。传入正则表达式soup.find_all(href=re.compile("elsie")),这个将会查找所有href属性满足正则表达式的标签
传入多个值:soup.find_all(id='link2',class_='title') ,这个将会查找到同时满足这两个属性的标签,这里的class必须用class_传入参数,因为class是python中的关键词

有些属性不能通过以上方法直接搜索,比如html5中的data-*属性,不过可以通过attrs参数指定一个字典参数来搜索包含特殊属性的标签,如下:

# [<p data-foo="value">foo!</p>]
data_soup.find_all(attrs={"data-foo": "value"})   #注意这里的atts不仅能够搜索特殊属性,亦可以搜索普通属性
soup.find_all("p",attrs={&#39;class&#39;:&#39;title&#39;,&#39;id&#39;:&#39;value&#39;})  #相当与soup.find_all(&#39;p&#39;,class_=&#39;title&#39;,id=&#39;value&#39;)
登录后复制

3.text参数:通过 text 参数可以搜搜文档中的字符串内容.与 name 参数的可选值一样, text 参数接受 字符串 , 正则表达式 , 列表, True

soup.find_all(text="Elsie")
# [u&#39;Elsie&#39;]
 
soup.find_all(text=["Tillie", "Elsie", "Lacie"])
# [u&#39;Elsie&#39;, u&#39;Lacie&#39;, u&#39;Tillie&#39;]
 
soup.find_all(text=re.compile("Dormouse"))
[u"The Dormouse&#39;s story", u"The Dormouse&#39;s story"]
登录后复制

4.limit参数:find_all() 方法返回全部的搜索结构,如果文档树很大那么搜索会很慢.如果我们不需要全部结果,可以使用 limit 参数限制返回结果的数量.效果与SQL中的limit关键字类似,当搜索到的结果数量达到 limit 的限制时,就停止搜索返回结果.

文档树中有3个tag符合搜索条件,但结果只返回了2个,因为我们限制了返回数量,代码如下:

soup.find_all("a", limit=2)
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
#  <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]
登录后复制

5.recursive 参数:调用tag的 find_all() 方法时,BeautifulSoup会检索当前tag的所有子孙节点,如果只想搜索tag的直接子节点,可以使用参数 recursive=False

find( name , attrs , recursive , text , **kwargs )
登录后复制

它与 find_all() 方法唯一的区别是 find_all() 方法的返回结果是值包含一个元素的列表,而 find() 方法直接返回结果,就是直接返回第一匹配到的元素,不是列表,不用遍历,如soup.find("p").get("class")

css选择器

我们在写 CSS 时,标签名不加任何修饰,类名前加点,id名前加#,在这里我们也可以利用类似的方法来筛选元素,用到的方法是 soup.select(),返回类型是 list

通过标签名查找

print soup.select(&#39;title&#39;) 
#[<title>The Dormouse&#39;s story</title>]
print soup.select(&#39;a&#39;)
#[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
登录后复制

通过类名查找

print soup.select(&#39;.sister&#39;)
#[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
登录后复制

通过id名查找

print soup.select(&#39;#link1&#39;)
#[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>]
登录后复制

组合查找

学过css的都知道css选择器,如p #link1是查找p标签下的id属性为link1的标签

print soup.select(&#39;p #link1&#39;)    #查找p标签中内容为id属性为link1的标签
#[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>]
print soup.select("head > title")   #直接查找子标签
#[<title>The Dormouse&#39;s story</title>]
登录后复制

属性查找

查找时还可以加入属性元素,属性需要用中括号括起来,注意属性和标签属于同一节点,所以中间不能加空格,否则会无法匹配到。

print soup.select(&#39;a[class="sister"]&#39;)
#[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
print soup.select(&#39;a[href="http://example.com/elsie"]&#39;)
#[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>]
登录后复制

同样,属性仍然可以与上述查找方式组合,不在同一节点的空格隔开,同一节点的不加空格,代码如下:

print soup.select(&#39;p a[href="http://example.com/elsie"]&#39;)
#[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>]
登录后复制

以上的 select 方法返回的结果都是列表形式,可以遍历形式输出,然后用 get_text() 方法来获取它的内容

soup = BeautifulSoup(html, &#39;lxml&#39;)
print type(soup.select(&#39;title&#39;))
print soup.select(&#39;title&#39;)[0].get_text()
for title in soup.select(&#39;title&#39;):
    print title.get_text()
登录后复制

修改文档树

Beautiful
Soup的强项是文档树的搜索,但同时也可以方便的修改文档树,这个虽说对于一些其他的爬虫并不适用,因为他们都是爬文章的内容的,并不需要网页的源码并且修改它们

修改tag的名称和属性

html="""
<p><a href=&#39;#&#39;>修改文档树</a></p>
"""
soup=BeautifulSoup(html,&#39;lxml&#39;)
tag=soup.a    #得到标签a,可以使用print tag.name输出标签
tag[&#39;class&#39;]=&#39;content&#39;    #修改标签a的属性class和p
tag[&#39;p&#39;]=&#39;nav&#39;
登录后复制

修改.string

注意这里如果标签的中还嵌套了子孙标签,那么如果直接使用string这个属性会将这里的所有的子孙标签都覆盖掉

html="""
<p><a href=&#39;#&#39;>修改文档树</a></p>
"""
soup=BeautifulSoup(html,&#39;lxml&#39;)
tag=soup.a
tag.string=&#39;博客&#39;   #这里会将修改文档树变成修改的内容
print  tag
soup.p.string=&#39;博客&#39;   #这里修改了p标签的内容,那么就会覆盖掉a标签,直接变成的修改后的文本
print soup
登录后复制

append

append的方法的作用是在在原本标签文本后面附加文本,就像python中列表的append方法

html="""
<p><a href=&#39;#&#39;>修改文档树</a></p>
"""
soup=BeautifulSoup(html,&#39;lxml&#39;)
soup.a.append("博客")    #在a标签和面添加文本,这里的文本内容将会变成修改文档树陈加兵的博客
print soup
print soup.a.contents    #这里输出a标签的内容,这里的必定是一个带有两个元素的列表
登录后复制

注意这里的append方法也可以将一个新的标签插入到文本的后面,下面将会讲到

new_tag

相信学过js的朋友都知道怎样创建一个新的标签,这里的方法和js中的大同小异,使用的new_tag
html="""
<p><p>
"""
soup=BeautifulSoup(html,&#39;lxml&#39;)
tag=soup.p
new_tag=soup.new_tag(&#39;a&#39;)    #创建一个新的标签a
new_tag[&#39;href&#39;]=&#39;#&#39;    #添加属性
new_tag.string=&#39;博客&#39;  #添加文本
print new_tag      
tag.append(new_tag)    #将新添加的标签写入到p标签中
print tag
登录后复制

insert

Tag.insert() 方法与 Tag.append() 方法类似,区别是不会把新元素添加到父节点 .contents
属性的最后,而是把元素插入到指定的位置.与Python列表总的 .insert() 方法的用法下同:
html="""
<p><p>
"""
soup=BeautifulSoup(html,&#39;lxml&#39;)
tag=soup.p
new_tag=soup.new_tag(&#39;a&#39;)
new_tag[&#39;href&#39;]=&#39;#&#39;
new_tag.string=&#39;博客&#39;
tag.append("欢迎来到")  #这里向p标签中插入文本,这个文本在contents下的序号为0
tag.insert(1,new_tag)   #在contents序号为1的位置插入新的标签,如果这里修改成0,那么将会出现a标签将会出现在欢饮来到的前面
print tag
登录后复制
注意这的1是标签的内容在contents中的序号,可以用print tag.contents查看当前的内容

insert_before() 和 insert_after()

insert_before() 方法在当前tag或文本节点前插入内容,insert_after() 方法在当前tag或文本节点后插入内容:

soup = BeautifulSoup("<b>stop</b>")
tag = soup.new_tag("i")
tag.string = "Don&#39;t"
soup.b.string.insert_before(tag)
soup.b
# <b><i>Don&#39;t</i>stop</b>
soup.b.i.insert_after(soup.new_string(" ever "))
soup.b
# <b><i>Don&#39;t</i> ever stop</b>
soup.b.contents
# [<i>Don&#39;t</i>, u&#39; ever &#39;, u&#39;stop&#39;]
登录后复制

clear

clear用来移除当前节点的所有的内容,包括其中的子孙节点和文本内容

html="""
<p><p>
"""
soup=BeautifulSoup(html,&#39;lxml&#39;)
tag=soup.p
new_tag=soup.new_tag(&#39;a&#39;)
new_tag[&#39;href&#39;]=&#39;#&#39;
new_tag.string=&#39;博客&#39;
tag.append("欢迎来到")
tag.insert(1,new_tag)
tag.clear()    #这里将会移除所有内容
print tag
登录后复制

以上就是python中BeautifulSoup的详细介绍(附代码)的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号