Python学习之beautifulsoup4库的使用


一、beautifulsoup4库简介

  使用requests库获取HTML页面并将其转换成字符串后,需要进一步分析HTML页面格式,提取有用信息,这个需要处理HTML和XML函数库。

  beautifulsoup4库,也成为Beautiful Soup库或者bs4库,用于解析和处理HTML和XML。需要注意的是,他不是Beautifulsoup库。它最大的优点是能根据HTML和XML语法建立解析树,进而高效解析其中的内容。

  HTML建立的web页面一般是比较复杂的,除了有用的信息外,还包括大量的页面格式信息元素,直接解析一个web网页需要深入了解HTML语法。而beautifulsoup4库将专业的web网页格式解析部分封装成函数,提供了若干有用快捷的处理函数。

二、beautifulsoup4库使用

1. beautifulsoup4库中最主要的是BeautifulSoup类,每个实例化的对象相当于一个页面。

使用BeautifulSoup()创建一个BeautifulSoup对象:

    

  创建的BeautifulSoup对象是一个树形结构,它包含HTML页面中的每一个Tag(标签)元素,如、等。具体描述的话,是HTML中主要结构都变成了BeautifulSoup对象的一个属性,

可以直接用.形式获取,其中的名字采用HTML中标签的名字。

2. BeautifulSoup对象常用的属性

     head          HTML页面的内容

     title             HTML页面标题,在中,由标记<br></span></p> <p><span style="font-size: 18px">     body           HTML页面的<body>内容</span></p> <p><span style="font-size: 18px">     p            HTML页面中第一个<p>内容</span></p> <p><span style="font-size: 18px">     strings           HTML页面所有呈现在web上的字符,即标签的内容</span></p> <p><span style="font-size: 18px">     stripped_strings       HTML页面所有呈现在web上的非空字符串</span></p> <p><span style="font-size: 18px">如略去<style>标签输出:</span></p> <p><span style="font-size: 18px"><br></span></p> <p><span style="font-size: 18px">  每一个Tag标签在beautifulsoup4库中都是一个对象,称为Tag对象。例如,title是一个标签对象。每一个标签对象在HTML中都有类似的结构:</span></p> <p><span style="font-size: 18px">  <a  class=''mnav'' href="http://www.baidu.com">糯米</a></span></p> <p><span style="font-size: 18px">其中,尖括号(<>)中标签的名字是name,尖括号内其他项是attrs,尖括号之间的内容是string。因此,Tag对象有属性name、attrs、string,并且采用<a>.<b>的语法形式</span></p> <p><span style="font-size: 18px">    name      字符串标签的名字,比如div</span></p> <p><span style="font-size: 18px">    attrs        字典,包含了原来页面Tag所有属性,比如href<br></span></p> <p><span style="font-size: 18px">    contents     列表,这个Tag下所有子Tag的内容</span></p> <p><span style="font-size: 18px">    string      字符串,Tag所包围的文本,网页中真实的文字</span></p> <p><span style="font-size: 18px">如下:</span></p> <p></p> <p>  <span style="font-size: 18px">由于HTML语法可以在标签中嵌套其他标签,所以string属性的返回值遵循如下原则:</span></p> <p><span style="font-size: 18px">(1)如果标签内部没有其他标签,string属性返回其中内容。</span></p> <p><span style="font-size: 18px">(2)如果标签内部还有其他标签,但只有一个标签,string属性返回最里面的标签内容。</span></p> <p><span style="font-size: 18px">(3)如果标签内部有超过一层嵌套标签,string属性返回None(空字符串</span><span style="font-size: 18px">)</span></p> <p><span style="font-size: 18px">HTML语法中同一个标签会有很多内容例如标签<a>,百度首页一共有13处,直接调用soup.a只能返回一个。当需要列出标签对应所有内容或者需要找到非第一个标签时,</span></p> <p><span style="font-size: 18px">需要用到BeautifulSoup的find()和find_all()方法。这两个方法会遍历到整个HTML文档,按照条件返回标签内容。</span></p> <p><span style="font-size: 18px">作用:根据参数找到标签对象,返回列表类型。</span></p> <p><span style="font-size: 18px">参数如下:</span></p> <p><span style="font-size: 18px">    name:按照Tag标签名字检索,名字用字符串形式表示,例如div、li;</span></p> <p><span style="font-size: 18px">    attrs:按照Tag标签属性值检索,需要列出属性名称和值,采用JSON表示;</span></p> <p><span style="font-size: 18px">    recursive:设置查找层次,只查找当前标签下一层时使用recursive=False;</span></p> <p><span style="font-size: 18px">    string:按照关键字检索string属性内容,采用string=开始;</span></p> <p><span style="font-size: 18px">    limit:返回结果的个数,默认返回全部结果;</span></p> <p><span style="font-size: 18px">如下:</span></p> <p><span style="font-size: 18px"></span></p> <p><span style="font-size: 18px">  简单的说,BeautifulSoup的find_all()方法可以根据标签名字、标签属性和内容检索并返回标签列表,通过片段字符串检索时需要使用正则表达式re函数库。当对标签进行检索时,</span></p> <p><span style="font-size: 18px">属性和对应的值采用JSON格式,如:</span></p> <p><span style="font-size: 18px">  ‘src’:re.compile('jquery')    其中键值对中值的部分可以是字符串或者正则表达式。</span></p> <p><span style="font-size: 18px">仅用于Python学习笔记</span></p> </div> <!--conend--> <div class="p-2"></div> <div class="arcinfo my-3 fs-7 text-center"> <a href='/t/etagid4460-0.html' class='tagbtn' target='_blank'>python学习</a> </div> <div class="p-2"></div> </div> <div class="p-2"></div> <!--xg--> <div class="lbox p-4 shadow-sm rounded-3"> <div class="boxtitle"><h2 class="fs-4">相关</h2></div> <hr> <div class="row g-0 py-2 border-bottom align-items-center"> <div class="col-7 col-lg-11 border-lg-end"> <h3 class="fs-6 mb-0 mb-lg-2"><a href="/a/1-562408.html">python学习(十)函数 内容</a></h3> <div class="ltag fs-8 d-none d-lg-block"> </div> </div> </div><div class="row g-0 py-2 border-bottom align-items-center"> <div class="col-7 col-lg-11 border-lg-end"> <h3 class="fs-6 mb-0 mb-lg-2"><a href="/a/1-562250.html">selenium python学习笔记之八 窗口截图、验证码处理</a></h3> <div class="ltag fs-8 d-none d-lg-block"> </div> </div> </div><div class="row g-0 py-2 border-bottom align-items-center"> <div class="col-7 col-lg-11 border-lg-end"> <h3 class="fs-6 mb-0 mb-lg-2"><a href="/a/1-553979.html">python学习随笔day15</a></h3> <div class="ltag fs-8 d-none d-lg-block"> </div> </div> </div><div class="row g-0 py-2 border-bottom align-items-center"> <div class="col-7 col-lg-11 border-lg-end"> <h3 class="fs-6 mb-0 mb-lg-2"><a href="/a/1-544474.html">【python学习笔记】openpyxl操作excel高阶操作</a></h3> <div class="ltag fs-8 d-none d-lg-block"> </div> </div> </div><div class="row g-0 py-2 border-bottom align-items-center"> <div class="col-7 col-lg-11 border-lg-end"> <h3 class="fs-6 mb-0 mb-lg-2"><a href="/a/1-516421.html">Python学习之PIL库的使用</a></h3> <div class="ltag fs-8 d-none d-lg-block"> </div> </div> </div><div class="row g-0 py-2 border-bottom align-items-center"> <div class="col-7 col-lg-11 border-lg-end"> <h3 class="fs-6 mb-0 mb-lg-2"><a href="/a/1-528817.html">Python学习笔记-----类和对象知识点2)---类的继承</a></h3> <div class="ltag fs-8 d-none d-lg-block"> </div> </div> </div><div class="row g-0 py-2 border-bottom align-items-center"> <div class="col-7 col-lg-11 border-lg-end"> <h3 class="fs-6 mb-0 mb-lg-2"><a href="/a/1-514753.html">Python学习笔记-----类和对象知识点2)---类的继承</a></h3> <div class="ltag fs-8 d-none d-lg-block"> </div> </div> </div><div class="row g-0 py-2 border-bottom align-items-center"> <div class="col-7 col-lg-11 border-lg-end"> <h3 class="fs-6 mb-0 mb-lg-2"><a href="/a/1-513759.html">【Python学习笔记六】获取百度搜索结果以及百度返回“百度安全验证”问题解决</a></h3> <div class="ltag fs-8 d-none d-lg-block"> </div> </div> </div><div class="row g-0 py-2 border-bottom align-items-center"> <div class="col-7 col-lg-11 border-lg-end"> <h3 class="fs-6 mb-0 mb-lg-2"><a href="/a/1-513628.html">【Python学习笔记七】从配置文件中读取参数</a></h3> <div class="ltag fs-8 d-none d-lg-block"> </div> </div> </div><div class="row g-0 py-2 border-bottom align-items-center"> <div class="col-7 col-lg-11 border-lg-end"> <h3 class="fs-6 mb-0 mb-lg-2"><a href="/a/1-514011.html">【Python学习】 把list, tuple中的元素用write()方法写入文件</a></h3> <div class="ltag fs-8 d-none d-lg-block"> </div> </div> </div><div class="row g-0 py-2 border-bottom align-items-center"> <div class="col-7 col-lg-11 border-lg-end"> <h3 class="fs-6 mb-0 mb-lg-2"><a href="/a/1-498167.html">python学习day37笔记</a></h3> <div class="ltag fs-8 d-none d-lg-block"> </div> </div> </div><div class="row g-0 py-2 border-bottom align-items-center"> <div class="col-7 col-lg-11 border-lg-end"> <h3 class="fs-6 mb-0 mb-lg-2"><a href="/a/1-493094.html">python学习day35笔记</a></h3> <div class="ltag fs-8 d-none d-lg-block"> </div> </div> </div> <!----> <!----> </div> <!--xgend--> </div> <div class="col-lg-3 col-12 p-0 ps-lg-2"> <!--box--> <!--boxend--> <!--<div class="p-2"></div>--> <!--box--> <div class="lbox p-4 shadow-sm rounded-3"> <div class="boxtitle pb-2"><h2 class="fs-4"><a href="#">标签</a></h2></div> <div class="clearfix"></div> <ul class="m-0 p-0 fs-7 r-tag"> </ul> <div class="clearfix"></div> </div> <!--box end--> </div> </div> </div> </main> <div class="p-2"></div> <footer> <div class="container-fluid p-0 bg-black"> <div class="container p-0 fs-8"> <p class="text-center m-0 py-2 text-white-50">一品网 <a class="text-white-50" href="https://beian.miit.gov.cn/" target="_blank">冀ICP备14022925号-6</a></p> </div> </div> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?6e3dd49b5f14d985cc4c6bdb9248f52b"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> </footer> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/js/bootstrap.min.js"></script> </body> </html>