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("./baidu.html","rb") #打开一个文档
html = file.read().decode("utf-8") #读取文档内容
bs = BeautifulSoup(html,"html.parser") #使用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)) #返回<class 'bs4.element.Tag'></p>
<h1>1.Tag 标签及其内容,拿到找到的第一个内容</h1>
<h1>print(bs.title.string) #获得标签里的内容,返回 百度一下,你就知道</h1>
<p>print(type(bs.title.string)) #返回<class 'bs4.element.NavigableString'></p>
<h1>2.NavigableString 标签里的内容(字符串)</h1>
<h1>print(bs.a.attrs) #返回标签内所有属性(以字典的形式)</h1>
<p>print(type(bs.a.attrs)) #返回 <class 'dict'></p>
<p>print(type(bs)) #返回 <class 'bs4.BeautifulSoup'></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)) #返回 <class 'bs4.element.Comment'></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("a")</h1>
<h1>正则表达式搜索:使用search()方法来匹配内容</h1>
<p>import re</p>
<h1>针对标签的字符过滤查找,返回与标签字符串匹配(只要标签中含有对应字符串)的标签以内的内容</h1>
<h1>t_list = bs.find_all(re.compile("a"))</h1>
<h1>方法:传入一个函数(方法),根据函数的要求来搜索</h1>
<p>'''
def name_is_exists(tag):
return tag.has_attr("name") #标签内容中含有 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="head") #返回有id="head" 参数在内的内容</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="贴吧") #利用text直接返回对应匹配的文本</h1>
<h1>t_list = bs.find_all(text=["贴吧","hao123","地图"]) #利用text直接返回对应匹配的文本</h1>
<p>t_list = bs.find_all(text=re.compile("\d")) #应用正则表达式查找包含特定文本的内容(标签里的字符串),\d 是指数字
for item in t_list:
print(item)
'''</p>
<h1>4.limit 参数</h1>
<p>'''
t_list = bs.find_all("a",limit=4) #limit限制返回数量
for item in t_list:
print(item)
'''</p>
<h1>5.CSS选择器</h1>
<p>t_list = bs.select("title") #通过标签来查找
t_list = bs.select(".mnav") #通过类名(class="mnav" )来查找
t_list = bs.select("#u1") #通过id(id="u1">)来查找
t_list = bs.select("a[class='bri']") #通过属性来查找:"a[class='bri']") 是通过a标签中的class='bri' 属性来查找
t_list = bs.select("head > title") #通过子标签来查找,在head标签里找到title标签的内容
t_list = bs.select("div > a")</p>
<h1>for item in t_list:</h1>
<h1>print(item)</h1>
<p>t_list = bs.select(".mnav ~ .bri") #通过兄弟标签(与mnav同类级别的bri)来查找
print(t_list)
print(t_list[0])
print(t_list[0].get_text()) #get_text() 取文本内容</p>