当前位置: 首页 > news >正文

徐州教育平台网站建设服务器安装wordpress

徐州教育平台网站建设,服务器安装wordpress,网站建设课程设计论文,鞍山吧台目录二十二、Python爬虫的BS4解析库22.1 BS4下载安装22.2 BS4解析对象22.3 BS4常用语法1) Tag节点22.4 遍历节点22.5 find_all()与find()1) find_all()2) find()22.6 CSS选择器二十二、Python爬虫的BS4解析库 Beautiful Soup 简称 BS4#xff08;其中 4 表示版本号#xff0… 目录二十二、Python爬虫的BS4解析库22.1 BS4下载安装22.2 BS4解析对象22.3 BS4常用语法1) Tag节点22.4 遍历节点22.5 find_all()与find()1) find_all()2) find()22.6 CSS选择器二十二、Python爬虫的BS4解析库 Beautiful Soup 简称 BS4其中 4 表示版本号是一个 Python 第三方库它可以从 HTML 或 XML 文档中快速地提取指定的数据。Beautiful Soup 语法简单使用方便并且容易理解因此您可以快速地学习并掌握它。 22.1 BS4下载安装 由于 Bautiful Soup 是第三方库因此需要单独下载下载方式非常简单执行以下命令即可安装 pip install bs4由于 BS4 解析页面时需要依赖文档解析器所以还需要安装 lxml 作为解析库 pip install lxmlPython 也自带了一个文档解析库 html.parser 但是其解析速度要稍慢于 lxml。除了上述解析器外还可以使用 html5lib解析器安装方式如下 pip install html5lib该解析器生成 HTML 格式的文档但速度较慢。 “解析器容错”指的是被解析的文档发生错误或不符合格式时通过解析器的容错性仍然可以按照既定的正确格式实现解析。 22.2 BS4解析对象 创建 BS4 解析对象是万事开头的第一步这非常地简单语法格式如下所示 #导入解析包 from bs4 import BeautifulSoup #创建beautifulsoup解析对象 soup BeautifulSoup(html_doc, html.parser)上述代码中html_doc 表示要解析的文档而 html.parser 表示解析文档时所用的解析器此处的解析器也可以是 ‘lxml’ 或者’html5lib’示例代码如下所示 #coding:utf8 html_doc htmlheadtitlec语言中文网/title/head body p classtitlebc.biancheng.net/b/p p classwebsite一个学习编程的网站 a hrefhttp://c.biancheng.net/python/ idlink1python教程/a a hrefhttp://c.biancheng.net/c/ idlink2c语言教程/afrom bs4 import BeautifulSoup soup BeautifulSoup(html_doc, html.parser) #prettify()用于格式化输出html/xml文档 print(soup.prettify())输出结果 html headtitlec语言中文网/title /head bodyp classtitlebc.biancheng.net/b/pp classwebsite一个学习编程的网站a hrefhttp://c.biancheng.net/python/ idlink1python教程/aa hrefhttp://c.biancheng.net/c/ idlink2c语言教程/a /body /html如果是外部文档您也可以通过 open() 的方式打开读取语法格式如下 soup BeautifulSoup(open(html_doc.html, encodingutf8), lxml)22.3 BS4常用语法 下面对爬虫中经常用到的 BS4 解析方法做详细介绍。 Beautiful Soup 将 HTML 文档转换成一个树形结构该结构有利于快速地遍历和搜索 HTML 文档。下面使用树状结构来描述一段 HTML 文档 htmlheadtitlec语言中文网/title/headh1c.biancheng.net/h1pb一个学习编程的网站/b/p/body/html树状图如下所示 图1HTML文档树结构图 文档树中的每个节点都是 Python 对象这些对象大致分为四类Tag , NavigableString , BeautifulSoup ,Comment 。其中使用最多的是 Tag 和 NavigableString。 Tag标签类HTML 文档中所有的标签都可以看做 Tag 对象。NavigableString字符串类指的是标签中的文本内容使用 text、string、strings 来获取文本内容。BeautifulSoup表示一个 HTML 文档的全部内容您可以把它当作一个人特殊的 Tag 对象。Comment表示 HTML 文档中的注释内容以及特殊字符串它是一个特殊的 NavigableString。 1) Tag节点 标签Tag是组成 HTML 文档的基本元素。在 BS4 中通过标签名和标签属性可以提取出想要的内容。看一组简单的示例 from bs4 import BeautifulSoup soup BeautifulSoup(p classWeb site urlbc.biancheng.net/b/p, html.parser) #获取整个p标签的html代码 print(soup.p) #获取b标签 print(soup.p.b) #获取p标签内容使用NavigableString类中的string、text、get_text() print(soup.p.text) #返回一个字典里面是多有属性和值 print(soup.p.attrs) #查看返回的数据类型 print(type(soup.p)) #根据属性获取标签的属性值返回值为列表 print(soup.p[class]) #给class属性赋值,此时属性值由列表转换为字符串 soup.p[class][Web,Site] print(soup.p)输出结果如下 soup.p输出结果: p classWeb site urlbc.biancheng.net/b/psoup.p.b输出结果 bc.biancheng.net/bsoup.p.text输出结果 c.biancheng.netsoup.p.attrs输出结果 {class: [Web, site, url]}type(soup.p)输出结果 class bs4.element.Tagsoup.p[class]输出结果 [Web, site, url]class属性重新赋值 p classWeb Sitebc.biancheng.net/b/p22.4 遍历节点 Tag 对象提供了许多遍历 tag 节点的属性比如 contents、children 用来遍历子节点parent 与 parents 用来遍历父节点而 next_sibling 与 previous_sibling 则用来遍历兄弟节点 。示例如下 #coding:utf8 from bs4 import BeautifulSouphtml_doc htmlheadtitlec语言中文网/title/head body p classtitlebc.biancheng.net/b/p p classwebsite一个学习编程的网站/p a hrefhttp://c.biancheng.net/python/ idlink1python教程/a, a hrefhttp://c.biancheng.net/c/ idlink2c语言教程/a andsoup BeautifulSoup(html_doc, html.parser) body_tagsoup.body print(body_tag) #以列表的形式输出所有子节点 print(body_tag.contents)输出结果 body p classtitlebc.biancheng.net/b/p p classwebsite一个学习编程的网站/p a hrefhttp://c.biancheng.net/python/ idlink1python教程/a, a hrefhttp://c.biancheng.net/c/ idlink2c语言教程/a and /body #以列表的形式输出 [\n, p classtitlebc.biancheng.net/b/p, \n, p classwebsite一个学习编程的网站/p, \n, a hrefhttp://c.biancheng.net/python/ idlink1python教程/a, \n, a hrefhttp://c.biancheng.net/c/ idlink2c语言教程/a, \n]Tag 的 children 属性会生成一个可迭代对象可以用来遍历子节点示例如下 for child in body_tag.children:print(child)输出结果 #注意此处已将换行符\n省略 p classtitlebc.biancheng.net/b/p p classwebsite一个学习编程的网站/p想了解更多相关示例可参考官方文档点击前往 22.5 find_all()与find() find_all() 与 find() 是解析 HTML 文档的常用方法它们可以在 HTML 文档中按照一定的条件相当于过滤器查找所需内容。find() 与 find_all() 的语法格式相似希望大家在学习的时候可以举一反三。 BS4 库中定义了许多用于搜索的方法find() 与 find_all() 是最为关键的两个方法其余方法的参数和使用与其类似。 1) find_all() find_all() 方法用来搜索当前 tag 的所有子节点并判断这些节点是否符合过滤条件最后以列表形式将符合条件的内容返回语法格式如下 find_all( name , attrs , recursive , text , limit )参数说明 name查找所有名字为 name 的 tag 标签字符串对象会被自动忽略。attrs按照属性名和属性值搜索 tag 标签注意由于 class 是 Python 的关键字吗所以要使用 “class_”。recursivefind_all() 会搜索 tag 的所有子孙节点设置 recursiveFalse 可以只搜索 tag 的直接子节点。text用来搜文档中的字符串内容该参数可以接受字符串 、正则表达式 、列表、True。limit由于 find_all() 会返回所有的搜索结果这样会影响执行效率通过 limit 参数可以限制返回结果的数量。 find_all() 使用示例如下 from bs4 import BeautifulSoup import rehtml_doc htmlheadtitlec语言中文网/title/head body p classtitlebc.biancheng.net/b/p p classwebsite一个学习编程的网站/p a hrefhttp://c.biancheng.net/python/ idlink1python教程/a a hrefhttp://c.biancheng.net/c/ idlink2c语言教程/a a hrefhttp://c.biancheng.net/django/ idlink3django教程/a p classvip加入我们阅读所有教程/p a hrefhttp://vip.biancheng.net/?fromindex idlink4成为vip/a#创建soup解析对象 soup BeautifulSoup(html_doc, html.parser) #查找所有a标签并返回 print(soup.find_all(a)) #查找前两条a标签并返回 print(soup.find_all(a,limit2)) #只返回两条a标签 最后以列表的形式返回输出结果如下所示 [a hrefhttp://c.biancheng.net/python/ idlink1python教程/a, a hrefhttp://c.biancheng.net/c/ idlink2c语言教程/a, a hrefhttp://c.biancheng.net/django/ idlink3django教程/a, a hrefhttp://vip.biancheng.net/?fromindex idlink4成为vip/a][a hrefhttp://c.biancheng.net/python/ idlink1python教程/a, a hrefhttp://c.biancheng.net/c/ idlink2c语言教程/a]按照标签属性以及属性值查找 HTML 文档如下所示 print(soup.find_all(p,class_website)) print(soup.find_all(idlink4))输出结果 [p classwebsite一个学习编程的网站/p] [a hrefhttp://vip.biancheng.net/?fromindex idlink4成为vip/a]正则表达式、列表以及 True 也可以当做过滤条件使用示例如下 #列表行书查找tag标签 print(soup.find_all([b,a])) #正则表达式匹配id属性值 print(soup.find_all(a,idre.compile(r.\d))) print(soup.find_all(idTrue)) #True可以匹配任何值下面代码会查找所有tag并返回相应的tag名称 for tag in soup.find_all(True):print(tag.name,end ) #输出所有以b开始的tag标签 for tag in soup.find_all(re.compile(^b)):print(tag.name)输出结果如下 第一个print输出 [bc.biancheng.net/b, a hrefhttp://c.biancheng.net/python/ idlink1python教程/a, a hrefhttp://c.biancheng.net/c/ idlink2c语言教程/a, a hrefhttp://c.biancheng.net/django/ idlink3django教程/a, a hrefhttp://vip.biancheng.net/?fromindex idlink4成为vip/a] 第二个print输出 [a hrefhttp://c.biancheng.net/python/ idlink1python教程/a, a hrefhttp://c.biancheng.net/c/ idlink2c语言教程/a, a hrefhttp://c.biancheng.net/django/ idlink3django教程/a, a hrefhttp://vip.biancheng.net/?fromindex idlink4成为vip/a] 第三个print输出 [a hrefhttp://c.biancheng.net/python/ idlink1python教程/a, a hrefhttp://c.biancheng.net/c/ idlink2c语言教程/a, a hrefhttp://c.biancheng.net/django/ idlink3django教程/a, a hrefhttp://vip.biancheng.net/?fromindex idlink4成为vip/a] 第四个print输出 html head title body p b p a a a p a 最后一个输出 body bBS4 为了简化代码为 find_all() 提供了一种简化写法如下所示 #简化前 soup.find_all(a) #简化后 soup(a)上述两种的方法的输出结果是相同的。 2) find() find() 方法与 find_all() 类似不同之处在于 find_all() 会将文档中所有符合条件的结果返回而 find() 仅返回一个符合条件的结果所以 find() 方法没有limit参数。使用示例如下 from bs4 import BeautifulSoup import rehtml_doc htmlheadtitlec语言中文网/title/head body p classtitlebc.biancheng.net/b/p p classwebsite一个学习编程的网站/p a hrefhttp://c.biancheng.net/python/ idlink1python教程/a a hrefhttp://c.biancheng.net/c/ idlink2c语言教程/a a hrefhttp://c.biancheng.net/django/ idlink3django教程/a p classvip加入我们阅读所有教程/p a hrefhttp://vip.biancheng.net/?fromindex idlink4成为vip/a#创建soup解析对象 soup BeautifulSoup(html_doc, html.parser) #查找第一个a并直接返回结果 print(soup.find(a)) #查找title print(soup.find(title)) #匹配指定href属性的a标签 print(soup.find(a,hrefhttp://c.biancheng.net/python/)) #根据属性值正则匹配 print(soup.find(class_re.compile(tit))) #attrs参数值 print(soup.find(attrs{class:vip}))输出结果如下 a标签 a hrefhttp://c.biancheng.net/python/ idlink1python教程/a 指定href属性 a hrefhttp://c.biancheng.net/python/ idlink1python教程/a title: titlec语言中文网/title 正则匹配 p classtitlebc.biancheng.net/b/p #attrs参数值 p classvip加入我们阅读所有教程/p使用 find() 时如果没有找到查询标签会返回 None而 find_all() 方法返回空列表。示例如下 print(soup.find(bdi)) print(soup.find_all(audio))输出结果如下 None []BS4 也为 find()提供了简化写法如下所示 #简化写法 print(soup.head.title) #上面代码等价于 print(soup.find(head).find(title))两种写法的输出结果相同如下所示 titlec语言中文网/title titlec语言中文网/title22.6 CSS选择器 BS4 支持大部分的 CSS 选择器比如常见的标签选择器、类选择器、id 选择器以及层级选择器。Beautiful Soup 提供了一个 select() 方法通过向该方法中添加选择器就可以在 HTML 文档中搜索到与之对应的内容。应用示例如下 #coding:utf8 html_doc htmlheadtitlec语言中文网/title/head body p classtitlebc.biancheng.net/b/p p classwebsite一个学习编程的网站/p a hrefhttp://c.biancheng.net/python/ idlink1python教程/a a hrefhttp://c.biancheng.net/c/ idlink2c语言教程/a a hrefhttp://c.biancheng.net/django/ idlink3django教程/a p classvip加入我们阅读所有教程/p a hrefhttp://vip.biancheng.net/?fromindex idlink4成为vip/a p classintroduce介绍: a hrefhttp://c.biancheng.net/view/8066.html idlink5关于网站/a a hrefhttp://c.biancheng.net/view/8092.html idlink6关于站长/a /pfrom bs4 import BeautifulSoupsoup BeautifulSoup(html_doc, html.parser) #根据元素标签查找 print(soup.select(title)) #根据属性选择器查找 print(soup.select(a[href])) #根据类查找 print(soup.select(.vip)) #后代节点查找 print(soup.select(html head title)) #查找兄弟节点 print(soup.select(p a)) #根据id选择p标签的兄弟节点 print(soup.select(p ~ #link3)) #nth-of-type(n)选择器用于匹配同类型中的第n个同级兄弟元素 print(soup.select(p ~ a:nth-of-type(1))) #查找子节点 print(soup.select(p a)) print(soup.select(.introduce #link5))输出结果 第一个输出 [titlec语言中文网/title]第二个输出 [a hrefhttp://c.biancheng.net/python/ idlink1python教程/a, a hrefhttp://c.biancheng.net/c/ idlink2c语言教程/a, a hrefhttp://c.biancheng.net/django/ idlink3django教程/a, a hrefhttp://vip.biancheng.net/?fromindex idlink4成为vip/a, a hrefhttp://c.biancheng.net/view/8066.html idlink5关于网站/a, a hrefhttp://c.biancheng.net/view/8092.html idlink6关于站长/a]第三个输出 [p classvip加入我们阅读所有教程/p]第四个输出 [titlec语言中文网/title]第五个输出 [a hrefhttp://c.biancheng.net/python/ idlink1python教程/a, a hrefhttp://vip.biancheng.net/?fromindex idlink4成为vip/a]第六个输出 [a hrefhttp://c.biancheng.net/django/ idlink3django教程/a]第七个输出 [a hrefhttp://c.biancheng.net/python/ idlink1python教程/a]第八个输出 [a hrefhttp://c.biancheng.net/view/8066.html idlink5关于网站/a, a hrefhttp://c.biancheng.net/view/8092.html idlink6关于站长/a]最后的print输出 [a hrefhttp://c.biancheng.net/view/8066.html idlink5关于网站/a]如果想了解更多关于 BS4 库的使用方法可以参考官方文档
文章转载自:
http://www.morning.wfykn.cn.gov.cn.wfykn.cn
http://www.morning.kgrwh.cn.gov.cn.kgrwh.cn
http://www.morning.lmjkn.cn.gov.cn.lmjkn.cn
http://www.morning.lrprj.cn.gov.cn.lrprj.cn
http://www.morning.sfqtf.cn.gov.cn.sfqtf.cn
http://www.morning.qxmys.cn.gov.cn.qxmys.cn
http://www.morning.4q9h.cn.gov.cn.4q9h.cn
http://www.morning.xmbhc.cn.gov.cn.xmbhc.cn
http://www.morning.zlgbx.cn.gov.cn.zlgbx.cn
http://www.morning.mlnbd.cn.gov.cn.mlnbd.cn
http://www.morning.hhzdj.cn.gov.cn.hhzdj.cn
http://www.morning.fcrw.cn.gov.cn.fcrw.cn
http://www.morning.wwklf.cn.gov.cn.wwklf.cn
http://www.morning.ylqpp.cn.gov.cn.ylqpp.cn
http://www.morning.knqck.cn.gov.cn.knqck.cn
http://www.morning.hpnhl.cn.gov.cn.hpnhl.cn
http://www.morning.kpmxn.cn.gov.cn.kpmxn.cn
http://www.morning.rwtlj.cn.gov.cn.rwtlj.cn
http://www.morning.kpygy.cn.gov.cn.kpygy.cn
http://www.morning.bxbnf.cn.gov.cn.bxbnf.cn
http://www.morning.jtqxs.cn.gov.cn.jtqxs.cn
http://www.morning.bsqth.cn.gov.cn.bsqth.cn
http://www.morning.brxzt.cn.gov.cn.brxzt.cn
http://www.morning.jmbfx.cn.gov.cn.jmbfx.cn
http://www.morning.wflpj.cn.gov.cn.wflpj.cn
http://www.morning.qqfcf.cn.gov.cn.qqfcf.cn
http://www.morning.nshhf.cn.gov.cn.nshhf.cn
http://www.morning.cnxpm.cn.gov.cn.cnxpm.cn
http://www.morning.tfsyk.cn.gov.cn.tfsyk.cn
http://www.morning.xznrk.cn.gov.cn.xznrk.cn
http://www.morning.ltcnd.cn.gov.cn.ltcnd.cn
http://www.morning.zsfooo.com.gov.cn.zsfooo.com
http://www.morning.jghty.cn.gov.cn.jghty.cn
http://www.morning.ntnml.cn.gov.cn.ntnml.cn
http://www.morning.iterlog.com.gov.cn.iterlog.com
http://www.morning.yprjy.cn.gov.cn.yprjy.cn
http://www.morning.nwcgj.cn.gov.cn.nwcgj.cn
http://www.morning.kynf.cn.gov.cn.kynf.cn
http://www.morning.zdzgf.cn.gov.cn.zdzgf.cn
http://www.morning.kxscs.cn.gov.cn.kxscs.cn
http://www.morning.nnykz.cn.gov.cn.nnykz.cn
http://www.morning.smrkf.cn.gov.cn.smrkf.cn
http://www.morning.llxyf.cn.gov.cn.llxyf.cn
http://www.morning.cmldr.cn.gov.cn.cmldr.cn
http://www.morning.sgfpn.cn.gov.cn.sgfpn.cn
http://www.morning.sjqpm.cn.gov.cn.sjqpm.cn
http://www.morning.yfmxn.cn.gov.cn.yfmxn.cn
http://www.morning.srwny.cn.gov.cn.srwny.cn
http://www.morning.hsjfs.cn.gov.cn.hsjfs.cn
http://www.morning.gjcdr.cn.gov.cn.gjcdr.cn
http://www.morning.hcsqznn.cn.gov.cn.hcsqznn.cn
http://www.morning.jjrsk.cn.gov.cn.jjrsk.cn
http://www.morning.btgxf.cn.gov.cn.btgxf.cn
http://www.morning.msgnx.cn.gov.cn.msgnx.cn
http://www.morning.pcshb.cn.gov.cn.pcshb.cn
http://www.morning.gycyt.cn.gov.cn.gycyt.cn
http://www.morning.wlgpz.cn.gov.cn.wlgpz.cn
http://www.morning.ypzsk.cn.gov.cn.ypzsk.cn
http://www.morning.xnqjs.cn.gov.cn.xnqjs.cn
http://www.morning.fwjfh.cn.gov.cn.fwjfh.cn
http://www.morning.lmrjn.cn.gov.cn.lmrjn.cn
http://www.morning.c7491.cn.gov.cn.c7491.cn
http://www.morning.yjmlg.cn.gov.cn.yjmlg.cn
http://www.morning.qjlnh.cn.gov.cn.qjlnh.cn
http://www.morning.iznek.com.gov.cn.iznek.com
http://www.morning.tblbr.cn.gov.cn.tblbr.cn
http://www.morning.qhln.cn.gov.cn.qhln.cn
http://www.morning.rzbgn.cn.gov.cn.rzbgn.cn
http://www.morning.xjmpg.cn.gov.cn.xjmpg.cn
http://www.morning.plhhd.cn.gov.cn.plhhd.cn
http://www.morning.fhtbk.cn.gov.cn.fhtbk.cn
http://www.morning.yrnll.cn.gov.cn.yrnll.cn
http://www.morning.cklgf.cn.gov.cn.cklgf.cn
http://www.morning.nuobeiergw.cn.gov.cn.nuobeiergw.cn
http://www.morning.gkdhf.cn.gov.cn.gkdhf.cn
http://www.morning.crsqs.cn.gov.cn.crsqs.cn
http://www.morning.hbqfh.cn.gov.cn.hbqfh.cn
http://www.morning.pqwrg.cn.gov.cn.pqwrg.cn
http://www.morning.jfqpc.cn.gov.cn.jfqpc.cn
http://www.morning.mgskc.cn.gov.cn.mgskc.cn
http://www.tj-hxxt.cn/news/251427.html

相关文章:

  • wordpress主题 king成都白帽优化
  • php完整电商网站开发源码派多格宠物网站建设
  • 网站运营的作用html页面网站建设中
  • 网站模版开发提供网站建设的理由
  • 电脑网站设计公司网站建设总结报告书
  • 国内外优秀网站设计WordPress总是收到英文评论
  • 南宁电商网站建设最有效的网站推广费用
  • 网站开发易语言哪个做网站公司好
  • 安徽网站seowordpress中文插件seo百度
  • 网上做夫妻的网站logo标志设计网
  • 网站会员系统wordpress网站素材类型
  • 普通网站备案建立网站商店
  • 网站ip流量查询网络用户管理系统注册
  • 网站静态和动态区别是什么意思做一个企业网站设计
  • flash网站什么意思做网站的好处在哪里
  • 电脑可以做服务器部署网站吗用.net做购物网站
  • 商务网站建设与推广实训意义计算机网络专业主要学什么
  • 在ppt里面做网站链接导出wordpress到文本
  • 自己搭建服务器 发布网站 域名如何申请黄骅住房和城乡建设局网站
  • 广州设计企业网站怎么样自己创建网站
  • 网站建设公司电话销售客源哪里找影楼风
  • 木马科技网站建设行业网站设计
  • 房地产网站建设意义成都网站建设推荐
  • 中国建设网站下载明年开春有望摘口罩
  • 网站开发虚拟电话汽车配件外贸网站
  • 公司百度网站怎么做如何做百度推广网站
  • 手机做网站用什么软件镇江市精神文明建设网站
  • 网站没有icp备案怎么访问wordpress 文件大小
  • 报名网站辽宁省建设银行北京网站建设专业公司
  • 怎么选择邯郸做网站广州开发区