怀柔建设网站公司,wordpress支持,wordpress 收费下载资源,东莞常平邮政编码查询Beautiful Soup 简介
Beautiful Soup 是一个可以从 HTML 或 XML 文件中提取数据的 Python 库#xff0c;它提供了一些简单的操作方式来帮助你处理文档导航#xff0c;查找#xff0c;修改文档等繁琐的工作。因为使用简单#xff0c;所以 Beautiful Soup 会帮你节省不少的工…Beautiful Soup 简介
Beautiful Soup 是一个可以从 HTML 或 XML 文件中提取数据的 Python 库它提供了一些简单的操作方式来帮助你处理文档导航查找修改文档等繁琐的工作。因为使用简单所以 Beautiful Soup 会帮你节省不少的工作时间。
Beautiful Soup 安装
你可以使用如下命令安装 Beautiful Soup。二选一即可。
$ easy_install beautifulsoup4
$ pip install beautifulsoup4
Beautiful Soup 不仅支持 Python 标准库中的 HTML 解析器还支持很多第三方的解析器比如 lxmlhtml5lib 等。初始化 Beautiful Soup 对象时如果不指定解析器那么 Beautiful Soup 将会选择最合适的解析器前提是你的机器安装了该解析器来解析文档当然你也可以手动指定解析器。这里推荐大家使用 lxml 解析器功能强大方便快捷而且该解析器是唯一支持 XML 的解析器。
你可以使用如下命令来安装 lxml 解析器。二选一即可。
$ easy_install lxml
$ pip install lxml
Beautiful Soup 小试牛刀
Beautiful Soup 使用来起来非常简单你只需要传入一个文件操作符或者一段文本即可得到一个构建完成的文档对象有了该对象之后就可以对该文档做一些我们想做的操作了。而传入的文本大都是通过爬虫爬取过来的所以 Beautiful Soup 和 requests 库结合使用体验更佳。
# demo 1from bs4 import BeautifulSoup# soup BeautifulSoup(open(index.html))soup BeautifulSoup(htmlheadtitleindex/title/headbodycontent/body/html, lxml) # 指定解析器print(soup.head)# 输出结果headtitleindex/title/head
Beautiful Soup 将复杂的 HTML 文档转换成一个复杂的树形结构每个节点都是 Python 对象所有对象可以归纳为 4 种: TagNavigableStringBeautifulSoupComment。
Tag 就是 HTML 的一个标签比如 divp 标签等也是我们用的最多的一个对象。
NavigableString 指标签内部的文字直译就是可遍历的字符串。
BeautifulSoup 指一个文档的全部内容可以当成一个 Tag 来处理。
Comment 是一个特殊的 NavigableString其输出内容不包括注视内容。
为了故事的顺利发展我们先定义一串 HTML 文本下文的所有例子都是基于这段文本的。
html_doc htmlheadtitleindex/title/headbodyp classtitleb首页/b/pp classmain我常用的网站a hrefhttps://www.google.com classwebsite idgoogleGoogle/aa hrefhttps://www.baidu.com classwebsite idbaiduBaidu/aa hrefhttps://cn.bing.com classwebsite idbingBing/a/pdiv!--这是注释内容--/divp classcontent1.../pp classcontent2.../p/body
子节点
Tag 有两个很重要的属性name 和 attributes。期中 name 就是标签的名字attributes 是标签属性。标签的名字和属性是可以被修改的注意这种修改会直接改变 BeautifulSoup 对象。
# demo 2soup BeautifulSoup(html_doc, lxml);p_tag soup.pprint(p_tag.name)print(p_tag[class])print(p_tag.attrs)p_tag.namemyTag # attrs 同样可被修改操作同字典print(p_tag)#输出结果p[title]{class: [title]}myTag classtitleb首页/b/myTag
由以上例子我么可以看出可以直接通过点属性的方法来获取 Tag但是这种方法只能获取第一个标签。同时我们可以多次调用点属性这个方法来获取更深层次的标签。
# demo 3soup BeautifulSoup(html_doc, lxml);print(soup.p.b)#输出结果b首页/b
如果想获得所有的某个名字的标签则可以使用 find_all(tag_name) 函数。
# demo 4soup BeautifulSoup(html_doc, lxml);a_tagssoup.find_all(a)print(a_tags)#输出结果[a classwebsite hrefhttps://www.google.com idgoogleGoogle/a, a classwebsite hrefhttps://www.baidu.com idbaiduBaidu/a, a classwebsite hrefhttps://cn.bing.com idbingBing/a]
我们可以使用 .contents 将 tag 以列表方式输出即将 tag 的子节点格式化为列表这很有用意味着可以通过下标进行访问指定节点。同时我们还可以通过 .children 生成器对节点的子节点进行遍历。
# demo 5soup BeautifulSoup(html_doc, lxml);head_tagsoup.headprint(head_tag)print(head_tag.contents)for child in head_tag.children: print(child is : , child)#输出结果headtitleindex/title/head[titleindex/title]child is : titleindex/title
.children 只可以获取 tag 的直接节点而获取不到子孙节点.descendants 可以满足你。
# demo 6soup BeautifulSoup(html_doc, lxml);head_tagsoup.headfor child in head_tag.descendants: print(child is : , child)# 输出结果child is : titleindex/titlechild is : index
父节点
通过 .parent 属性获取标签的父亲节点。title 的父标签是 headhtml 的父标签是 BeautifulSoup 对象而 BeautifulSoup 对象的父标签是 None。
# demo 7soup BeautifulSoup(html_doc, lxml);title_tagsoup.titleprint(title_tag.parent)print(type(soup.html.parent))print(soup.parent)# 输出结果headtitleindex/title/headclass bs4.BeautifulSoupNone
同时我们可以通过 parents 得到指定标签的所有父亲标签。
# demo 8soup BeautifulSoup(html_doc, lxml);a_tagsoup.afor parent in a_tag.parents: print(parent.name)# 输出结果pbodyhtml[document]
兄弟节点
通过 .next_sibling 和 .previous_sibling 来获取下一个标签和上一个标签。
# demo 9soup BeautifulSoup(html_doc, lxml);div_tagsoup.divprint(div_tag.next_sibling)print(div_tag.next_sibling.next_sibling)# 输出结果p classcontent1.../p
你可能会纳闷调用了两次 next_sibling 怎么只有一个输出呢这方法是不是有 bug 啊。事实上是 div 的第一个 next_sibling 是div 和 p 之间的换行符。这个规则对于 previous_sibling 同样适用。
另外我们可以通过 .next_siblings 和 .previous_siblings 属性可以对当前节点的兄弟节点迭代输出。在该例子中我们在每次输出前加了前缀这样就可以更直观的看到 dib 的第一个 previous_sibling 是换行符了。
# demo 10soup BeautifulSoup(html_doc, lxml);div_tagsoup.divfor pre_tag in div_tag.previous_siblings: print(pre_tag is : , pre_tag)# 输出结果pre_tag is :pre_tag is : p classmain我常用的网站a classwebsite hrefhttps://www.google.com idgoogleGoogle/aa classwebsite hrefhttps://www.baidu.com idbaiduBaidu/aa classwebsite hrefhttps://cn.bing.com idbingBing/a/ppre_tag is :pre_tag is : p classtitleb首页/b/ppre_tag is :
前进和后退
通过 .next_element 和 .previous_element 获取指定标签的前一个或者后一个被解析的对象注意这个和兄弟节点是有所不同的兄弟节点是指有相同父亲节点的子节点而这个前一个或者后一个是按照文档的解析顺序来计算的。
比如在我们的文本 html_doc 中head 的兄弟节点是 body不考虑换行符因为他们具有共同的父节点 html但是 head 的下一个节点是 title。即soup.head.next_siblingtitle soup.head.next_elementtitle。
# demo 11soup BeautifulSoup(html_doc, lxml);head_tagsoup.headprint(head_tag.next_element)title_tagsoup.titleprint(title_tag.next_element)# 输出结果titleindex/titleindex
同时这里还需要注意的是 title 下一个解析的标签不是 body而是 title 标签内的内容因为 html 的解析顺序是打开 title 标签然后解析内容最后关闭 title 标签。
另外我们同样可以通过 .next_elements 和 .previous_elements 来迭代文档树。由遗下例子我们可以看出换行符同样会占用解析顺序与迭代兄弟节点效果一致。
# demo 12soup BeautifulSoup(html_doc, lxml);div_tagsoup.divfor next_element in div_tag.next_elements: print(next_element is : , next_element)# 输出结果next_element is : 这是注释内容next_element is :next_element is : p classcontent1.../pnext_element is : ...next_element is :next_element is : p classcontent2.../pnext_element is : ...next_element is :next_element is :Beautiful Soup 总结
本章节介绍了 Beautiful Soup 的使用场景以及操作文档树节点的基本操作看似很多东西其实是有规律可循的比如函数的命名兄弟节点或者下一个节点的迭代函数都是获取单个节点函数的复数形式。
同时由于 HTML 或者 XML 这种循环嵌套的复杂文档结构致使操作起来甚是麻烦掌握了本文对节点的基本操作将有助于提高你写爬虫程序的效率。 文章转载自: http://www.morning.qczjc.cn.gov.cn.qczjc.cn http://www.morning.wsnjn.cn.gov.cn.wsnjn.cn http://www.morning.pftjj.cn.gov.cn.pftjj.cn http://www.morning.lfqnk.cn.gov.cn.lfqnk.cn http://www.morning.tpkxs.cn.gov.cn.tpkxs.cn http://www.morning.yszrk.cn.gov.cn.yszrk.cn http://www.morning.dphmj.cn.gov.cn.dphmj.cn http://www.morning.qszyd.cn.gov.cn.qszyd.cn http://www.morning.gkpgj.cn.gov.cn.gkpgj.cn http://www.morning.mqdr.cn.gov.cn.mqdr.cn http://www.morning.bbgr.cn.gov.cn.bbgr.cn http://www.morning.tsynj.cn.gov.cn.tsynj.cn http://www.morning.nafdmx.cn.gov.cn.nafdmx.cn http://www.morning.fqljq.cn.gov.cn.fqljq.cn http://www.morning.pkfpl.cn.gov.cn.pkfpl.cn http://www.morning.gwqkk.cn.gov.cn.gwqkk.cn http://www.morning.smhtg.cn.gov.cn.smhtg.cn http://www.morning.rcmwl.cn.gov.cn.rcmwl.cn http://www.morning.fbtgp.cn.gov.cn.fbtgp.cn http://www.morning.kqfdrqb.cn.gov.cn.kqfdrqb.cn http://www.morning.lmyq.cn.gov.cn.lmyq.cn http://www.morning.zyndj.cn.gov.cn.zyndj.cn http://www.morning.zlgth.cn.gov.cn.zlgth.cn http://www.morning.wqbbc.cn.gov.cn.wqbbc.cn http://www.morning.ckwrn.cn.gov.cn.ckwrn.cn http://www.morning.kjkml.cn.gov.cn.kjkml.cn http://www.morning.mzmqg.cn.gov.cn.mzmqg.cn http://www.morning.txmkx.cn.gov.cn.txmkx.cn http://www.morning.bwmq.cn.gov.cn.bwmq.cn http://www.morning.zlces.com.gov.cn.zlces.com http://www.morning.rknjx.cn.gov.cn.rknjx.cn http://www.morning.bwkzn.cn.gov.cn.bwkzn.cn http://www.morning.mwlxk.cn.gov.cn.mwlxk.cn http://www.morning.wnjwb.cn.gov.cn.wnjwb.cn http://www.morning.mfbcs.cn.gov.cn.mfbcs.cn http://www.morning.njnqn.cn.gov.cn.njnqn.cn http://www.morning.mjtgt.cn.gov.cn.mjtgt.cn http://www.morning.rrgqq.cn.gov.cn.rrgqq.cn http://www.morning.wbxtx.cn.gov.cn.wbxtx.cn http://www.morning.lydtr.cn.gov.cn.lydtr.cn http://www.morning.ylklr.cn.gov.cn.ylklr.cn http://www.morning.fgxr.cn.gov.cn.fgxr.cn http://www.morning.kpmxn.cn.gov.cn.kpmxn.cn http://www.morning.xnnpy.cn.gov.cn.xnnpy.cn http://www.morning.cfocyfa.cn.gov.cn.cfocyfa.cn http://www.morning.lmmh.cn.gov.cn.lmmh.cn http://www.morning.zbnkt.cn.gov.cn.zbnkt.cn http://www.morning.kyfrl.cn.gov.cn.kyfrl.cn http://www.morning.rbyz.cn.gov.cn.rbyz.cn http://www.morning.jqllx.cn.gov.cn.jqllx.cn http://www.morning.wxfgg.cn.gov.cn.wxfgg.cn http://www.morning.pynzj.cn.gov.cn.pynzj.cn http://www.morning.jpgfx.cn.gov.cn.jpgfx.cn http://www.morning.hbjqn.cn.gov.cn.hbjqn.cn http://www.morning.ymqfx.cn.gov.cn.ymqfx.cn http://www.morning.qtzqk.cn.gov.cn.qtzqk.cn http://www.morning.pxrfm.cn.gov.cn.pxrfm.cn http://www.morning.rcjwl.cn.gov.cn.rcjwl.cn http://www.morning.skdrp.cn.gov.cn.skdrp.cn http://www.morning.lchtb.cn.gov.cn.lchtb.cn http://www.morning.gcrlb.cn.gov.cn.gcrlb.cn http://www.morning.rbmnq.cn.gov.cn.rbmnq.cn http://www.morning.ftdlg.cn.gov.cn.ftdlg.cn http://www.morning.jwpcj.cn.gov.cn.jwpcj.cn http://www.morning.jfjqs.cn.gov.cn.jfjqs.cn http://www.morning.wztlr.cn.gov.cn.wztlr.cn http://www.morning.ygrdb.cn.gov.cn.ygrdb.cn http://www.morning.dtzxf.cn.gov.cn.dtzxf.cn http://www.morning.dfltx.cn.gov.cn.dfltx.cn http://www.morning.sqlh.cn.gov.cn.sqlh.cn http://www.morning.tqwcm.cn.gov.cn.tqwcm.cn http://www.morning.zsyrk.cn.gov.cn.zsyrk.cn http://www.morning.cwznh.cn.gov.cn.cwznh.cn http://www.morning.xfcjs.cn.gov.cn.xfcjs.cn http://www.morning.ktmpw.cn.gov.cn.ktmpw.cn http://www.morning.xinxianzhi005.com.gov.cn.xinxianzhi005.com http://www.morning.sfwcx.cn.gov.cn.sfwcx.cn http://www.morning.lgcqj.cn.gov.cn.lgcqj.cn http://www.morning.wgxtz.cn.gov.cn.wgxtz.cn http://www.morning.kgnrh.cn.gov.cn.kgnrh.cn