Python


bs4

<p>''' BeautifulSoup4将复杂HTML文档转换成一个复杂的树形结构,每个节点都是Python对象,所有对象可以归纳为4种:</p> <ul> <li>Tag 标签</li> <li>NavigableString -BeautifulSoup -Comment '''</li> </ul> <p>from bs4 import BeautifulSoup</p> <p>file = open(&quot;./baidu.html&quot;,&quot;rb&quot;) #打开一个文档 html = file.read().decode(&quot;utf-8&quot;) #读取文档内容 bs = BeautifulSoup(html,&quot;html.parser&quot;) #使用BeautifulSoup去解析文档内容 '''</p> <h1>print(bs.title) #返回 <title>百度一下,你就知道 </title></h1> <h1>print(bs.head)</h1> <h1>print(bs.a)</h1> <h1>print(bs.html)</h1> <h1>print(bs.div)</h1> <p>print(type(bs.head)) #返回&lt;class 'bs4.element.Tag'&gt;</p> <h1>1.Tag 标签及其内容,拿到找到的第一个内容</h1> <h1>print(bs.title.string) #获得标签里的内容,返回 百度一下,你就知道</h1> <p>print(type(bs.title.string)) #返回&lt;class 'bs4.element.NavigableString'&gt;</p> <h1>2.NavigableString 标签里的内容(字符串)</h1> <h1>print(bs.a.attrs) #返回标签内所有属性(以字典的形式)</h1> <p>print(type(bs.a.attrs)) #返回 &lt;class 'dict'&gt;</p> <p>print(type(bs)) #返回 &lt;class 'bs4.BeautifulSoup'&gt;</p> <h1>3. BeautifulSoup 表示整个文档</h1> <h1>print(bs.name) #返回 [document]</h1> <h1>print(bs.attrs)</h1> <h1>print(bs) #返回整个文档内容</h1> <h1>print(bs.a.string)</h1> <p>print(type(bs.a.string)) #返回 &lt;class 'bs4.element.Comment'&gt;</p> <h1>4.Comment 是一个特殊的 NavigableString ,但输出的内容不包含注释符号</h1> <p>'''</p> <h1>------------------------------------------------------</h1> <h1>文档的遍历</h1> <p>''' print(bs.head.contents) #以列表的形式返回head里面的内容 print(bs.head.contents[1]) #根据返回列表的内容进行筛选打印</p> <h1>参考学习:<a href="https://blog.csdn.net/qq_42554007/article/details/90675142">https://blog.csdn.net/qq_42554007/article/details/90675142</a></h1> <h1><a href="https://www.cnblogs.com/wlx97e6/p/9960272.html">https://www.cnblogs.com/wlx97e6/p/9960272.html</a></h1> <p>'''</p> <h1>文档搜索</h1> <h1>1.find_all()</h1> <h1>针对标签的字符过滤查找,返回与标签字符串完全匹配的标签以内的内容</h1> <h1>t_list = bs.find_all(&quot;a&quot;)</h1> <h1>正则表达式搜索:使用search()方法来匹配内容</h1> <p>import re</p> <h1>针对标签的字符过滤查找,返回与标签字符串匹配(只要标签中含有对应字符串)的标签以内的内容</h1> <h1>t_list = bs.find_all(re.compile(&quot;a&quot;))</h1> <h1>方法:传入一个函数(方法),根据函数的要求来搜索</h1> <p>''' def name_is_exists(tag): return tag.has_attr(&quot;name&quot;) #标签内容中含有 name 参数属性的内容 t_list = bs.find_all(name_is_exists)</p> <p>for item in t_list: print(item) '''</p> <h1>2.kwargs 参数</h1> <p>'''</p> <h1>t_list = bs.find_all(id=&quot;head&quot;) #返回有id=&quot;head&quot; 参数在内的内容</h1> <h1>t_list = bs.find<em>all(class</em>=True) #class_=True 是指匹配包含参数class 在内的内容</h1> <p>t_list = bs.find_all(href=True) ##href=True 是指匹配包含参数 href 在内的内容 for item in t_list: print(item) '''</p> <h1>3. text 参数</h1> <p>'''</p> <h1>t_list = bs.find_all(text=&quot;贴吧&quot;) #利用text直接返回对应匹配的文本</h1> <h1>t_list = bs.find_all(text=[&quot;贴吧&quot;,&quot;hao123&quot;,&quot;地图&quot;]) #利用text直接返回对应匹配的文本</h1> <p>t_list = bs.find_all(text=re.compile(&quot;\d&quot;)) #应用正则表达式查找包含特定文本的内容(标签里的字符串),\d 是指数字 for item in t_list: print(item) '''</p> <h1>4.limit 参数</h1> <p>''' t_list = bs.find_all(&quot;a&quot;,limit=4) #limit限制返回数量 for item in t_list: print(item) '''</p> <h1>5.CSS选择器</h1> <p>t_list = bs.select(&quot;title&quot;) #通过标签来查找 t_list = bs.select(&quot;.mnav&quot;) #通过类名(class=&quot;mnav&quot; )来查找 t_list = bs.select(&quot;#u1&quot;) #通过id(id=&quot;u1&quot;&gt;)来查找 t_list = bs.select(&quot;a[class='bri']&quot;) #通过属性来查找:&quot;a[class='bri']&quot;) 是通过a标签中的class='bri' 属性来查找 t_list = bs.select(&quot;head &gt; title&quot;) #通过子标签来查找,在head标签里找到title标签的内容 t_list = bs.select(&quot;div &gt; a&quot;)</p> <h1>for item in t_list:</h1> <h1>print(item)</h1> <p>t_list = bs.select(&quot;.mnav ~ .bri&quot;) #通过兄弟标签(与mnav同类级别的bri)来查找 print(t_list) print(t_list[0]) print(t_list[0].get_text()) #get_text() 取文本内容</p>

页面列表

ITEM_HTML