Python爬虫--beautifulsoup 4 用法
2017-06-08 13:32
393 查看
BeautifulSoup将复杂HTML文档转换成一个复杂的树形结构,
每个节点都是Python对象,所有对象可以归纳为4种:Tag,NavigableString,BeautifulSoup,Comment
一、Tag:
Tag对象与XML或HTML原生文档中的tag相同,Tag有很多方法和属性,最重要的是name和attributes
name:
每个Tag都有自己名字,通过.name来获取
tag.name
也可以改变这个名字,将影响所有通过当前BeautifulSoup对象生成的HTML文档:
tag.name="blockquote" #会改变它的名字
attribute:
一个tag可能有很多个属性.tag<bclass="boldest">有一个“class”的属性,
值为“boldest”.tag的属性的操作方法与字典相同:
也可以直接”点”取属性,比如:.attrs:
将tag转换成字符串时,多值属性会合并为一个值
如果转换的文档是XML格式,那么tag中不包含多值属性
字符串常被包含在tag内.BeautifulSoup用NavigableString类来包装tag中的字符串:
一个NavigableString字符串与Python中的Unicode字符串相同,并且还支持包含在
遍历文档树和搜索文档树中的一些特性.
tag中包含的字符串不能编辑,但是可以被替换成其它的字符串,用replace_with()方法:
因为BeautifulSoup对象并不是真正的HTML或XML的tag,所以它没有name和attribute属性.
但有时查看它的.name属性是很方便的,所以BeautifulSoup对象包含了一个值为“[document]”的特殊属性.name
Tag,NavigableString,BeautifulSoup几乎覆盖了html和xml中的所有内容,
但是还有一些特殊对象.容易让人担心的内容是文档的注释部分:
Comment对象是一个特殊类型的NavigableString对象:
但是当它出现在HTML文档中时,Comment对象会使用特殊的格式输出:
BeautifulSoup中定义的其它类型都可能会出现在XML的文档中:CData,ProcessingInstruction,Declaration,
Doctype.与Comment对象类似,这些类都是NavigableString的子类,只是添加了一些额外的方法的字符串独享.
下面是用CDATA来替代注释的例子:
每个节点都是Python对象,所有对象可以归纳为4种:Tag,NavigableString,BeautifulSoup,Comment
一、Tag:
Tag对象与XML或HTML原生文档中的tag相同,Tag有很多方法和属性,最重要的是name和attributes
soup=BeautifulSoup('<bclass="boldest">Extremelybold</b>') tag=soup.b type(tag)
name:
每个Tag都有自己名字,通过.name来获取
tag.name
也可以改变这个名字,将影响所有通过当前BeautifulSoup对象生成的HTML文档:
tag.name="blockquote" #会改变它的名字
attribute:
一个tag可能有很多个属性.tag<bclass="boldest">有一个“class”的属性,
值为“boldest”.tag的属性的操作方法与字典相同:
tag['class']
也可以直接”点”取属性,比如:.attrs:
tag.attrs Tag的属性操作方法与字典一样,也是键对应值,可以向下面一样
tag['class']='verybold' tag['id']=1 tag #<blockquoteclass="verybold"id="1">Extremelybold</blockquote> 还可以删除:
deltag['class'] deltag['id'] tag #<blockquote>Extremelybold</blockquote> 删除了就没有了:
tag['class'] #KeyError:'class' print(tag.get('class')) #None 多值属性 HTML4定义了一系列可以包含多个值的属性.在HTML5中移除了一些,却增加更多.最常见的多值的属性是 class(一个tag可以有多个CSS的class).还有一些属性rel,rev,accept-charset ,headers,accesskey.在BeautifulSoup中多值属性的返回类型是list:
css_soup=BeautifulSoup('<pclass="bodystrikeout"></p>') css_soup.p['class'] #["body","strikeout"]
css_soup=BeautifulSoup('<pclass="body"></p>') css_soup.p['class'] #["body"]
如果某个属性看起来好像有多个值,但在任何版本的HTML定义中都没有被定义为多值属性, 那么BeautifulSoup会将这个属性作为字符串返回
id_soup=BeautifulSoup('<pid="myid"></p>') id_soup.p['id'] #'myid'
将tag转换成字符串时,多值属性会合并为一个值
rel_soup=BeautifulSoup('<p>Backtothe<arel="index">homepage</a></p>') rel_soup.a['rel'] #['index'] rel_soup.a['rel']=['index','contents'] print(rel_soup.p) #<p>Backtothe<arel="indexcontents">homepage</a></p>
如果转换的文档是XML格式,那么tag中不包含多值属性
xml_soup=BeautifulSoup('<pclass="bodystrikeout"></p>','xml') xml_soup.p['class'] #u'bodystrikeout'
二、NavigableString
字符串常被包含在tag内.BeautifulSoup用NavigableString类来包装tag中的字符串:
tag.string #u'Extremelybold' type(tag.string) #<class'bs4.element.NavigableString'>
一个NavigableString字符串与Python中的Unicode字符串相同,并且还支持包含在
遍历文档树和搜索文档树中的一些特性.
unicode_string=encode('utf-8') unicode_string #u'Extremelybold' type(unicode_string) #<type'unicode'>
tag中包含的字符串不能编辑,但是可以被替换成其它的字符串,用replace_with(
tag.string.replace_with("Nolongerbold") tag #<blockquote>Nolongerbold</blockquote>
三、BeautifulSoup
BeautifulSoup对象表示的是一个文档的全部内容.大部分时候, 可以把它当作Tag对象,它支持遍历文档树和搜索文档树中描述的大部分的方法.
因为BeautifulSoup对象并不是真正的HTML或XML的tag,所以它没有name和attribute属性.
但有时查看它的.name属性是很方便的,所以BeautifulSoup对象包含了一个值为“[document]”的特殊属性.name
soup.name #u'[document]'
四、Comment
Tag,NavigableString,BeautifulSoup几乎覆盖了html和xml中的所有内容,
但是还有一些特殊对象.容易让人担心的内容是文档的注释部分:
markup="<b><!--Hey,buddy.Wanttobuyausedparser?--></b>" soup=BeautifulSoup(markup) comment=soup.b.string type(comment) #<class'bs4.element.Comment'>
Comment对象是一个特殊类型的NavigableString对象:
comment #u'Hey,buddy.Wanttobuyausedparser'
但是当它出现在HTML文档中时,Comment对象会使用特殊的格式输出:
print(soup.b.prettify()) #<b> #<!--Hey,buddy.Wanttobuyausedparser?--> #</b>
BeautifulSoup中定义的其它类型都可能会出现在XML的文档中:CData,ProcessingInstruction,Declaration,
Doctype.与Comment对象类似,这些类都是NavigableString的子类,只是添加了一些额外的方法的字符串独享.
下面是用CDATA来替代注释的例子:
frombs4importCData cdata=CData("ACDATAblock") comment.replace_with(cdata) print(soup.b.prettify()) #<b> #<![CDATA[ACDATAblock]]> #</b>
一、遍历文档树
相关文章推荐
- python简单爬虫 及 beautifulSoup简单用法
- python爬虫【记录】BeautifulSoup 的用法遍历学习
- python爬虫系列(2)—— requests和BeautifulSoup库的基本用法
- Python爬虫BeautifulSoup用法(1)
- 【爬虫】python之BeautifulSoup用法
- Python爬虫-BeautifulSoup4 库的一些用法
- python爬虫之BeautifulSoup的用法
- python爬虫--BeautifulSoup的简单用法
- Python爬虫辅助库BeautifulSoup4用法精要
- python 自己写爬虫 ----- BeautifulSoup
- 以视频爬取实例讲解Python爬虫神器Beautiful Soup用法
- Python爬虫入门八之Beautiful Soup的用法
- python beautifulsoup简单用法
- Python爬虫入门(8):Beautiful Soup的用法
- Python爬虫入门-Beautiful Soup的用法
- python的【爬虫】:使用urllib爬取wiki文章,使用beautifulSoup解析html
- Python爬虫入门四之Urllib库的高级用法
- Python爬虫入门四之Urllib库的高级用法
- Python爬虫入门八之Beautiful Soup的用法
- Python爬虫入门之Beautiful Soup的用法