beautifulsoup4
bs4解析庫是靈活又方便的網頁解析庫,處理高效,支持多種解析器。利用它不用編寫正則表達式即可方便地實現網頁的提取
要解析的html標簽
from bs4 import BeautifulSoup # 要解析的html標簽
html_str = """ <li data_group="server" class="content"> <a href="/commands.html" class="index" name="a1">第一個a標簽 <a href="/commands.html" class="index2" name="a2">第二個a標簽 <a href="/commands/flushdb.html"> <span class="first"> 這是第一個span標簽 <span class="second"> 這是第二個span標簽,第一個下的子span標簽 </span> </span> <span class="third">這是第三個span標簽</span> <h3>這是一個h3</h3> </a> </li> """
1. 找標簽:
# 1. find_all 找到所有的li標簽 結果為一個結果集
li_find_all = BeautifulSoup(html_str, "lxml").find_all("li") print(type(li_find_all)) # <class 'bs4.element.ResultSet'> # 2. find 找到第一個li標簽 結果為一個標簽對象
li_find = BeautifulSoup(html_str, "lxml").find("li") print(type(li_find)) # <class 'bs4.element.Tag'> # 添加限制條件 class id
li = BeautifulSoup(html_str, "lxml").find_all("li", class_="content", data_group="server") li1 = BeautifulSoup(html_str, "lxml").find_all("li", attrs={"class":"content", "data_group":"server"})
2. 找標簽屬性和name:
# 找到a標簽的屬性和name
a = BeautifulSoup(html_str, "lxml").find("a") print(a.get("href"), a.name, type(a.get("href"))) # /commands.html a <class 'str'>
print(a.attrs, type(a.attrs), a.text, a.string,a.get_text(), type(a.string)) # {'href': '/commands.html', 'class': ['index'], 'name': 'a1'} <class 'dict'> 第一個a標簽 <class 'bs4.element.NavigableString'>
3. 處理子標簽和后代標簽:
# 找到li下的后代標簽
li_find = BeautifulSoup(html_str, "lxml").find("li") print(li_find.children) # <list_iterator object at 0x00000132C0915320>
""" for i in li_find.children: print(type(i),i) """
# 找到li下的子標簽 返回第一個找到的標簽
print(li_find.a, type(li_find.a)) # <a class="index" href="/commands.html" name="a1">第一個a標簽</a> <class 'bs4.element.Tag'>
4. 處理兄弟標簽:
# 處理a標簽的兄弟
a = BeautifulSoup(html_str, "lxml").find("a", class_="index2") print(a.next_siblings, type(a.next_siblings)) # <generator object next_siblings at 0x000001B14AA712B0> <class 'generator'>
""" for i in a.next_siblings: print(i, type(i), "\n") 1. <a class="index" href="/commands.html" name="a1">第一個a標簽 </a> <class 'bs4.element.Tag'> 2. <a href="/commands/flushdb.html"> <span class="first"> 這是第一個span標簽 <span class="second"> 這是第二個span標簽,第一個下的子span標簽 </span> </span> <span class="third">這是第三個span標簽</span> <h3>這是一個h3</h3> </a> <class 'bs4.element.Tag'> """
# print("next--", a.last ,type(a.next)) # 一組兄弟標簽中的下一個標簽next_sibling() 下的所有標簽next_siblings() # 一組兄弟標簽中的上一個標簽previous_sibling() 上的所有標簽previous_siblings() # 找到一組兄弟標簽下的最后一個標簽:
a = [x for x in a.next_siblings][-1] print("aaaaaa", a, type(a))
5. 處理父標簽:
# 1.parent # 返回的父標簽及其子標簽
span = BeautifulSoup(html_str, "lxml").find("span", class_="second") print(span.parent, type(span.parent)) # 2. parents 一層一層返回
""" span = BeautifulSoup(html_str, "lxml").find("span", class_="second") for i in span.parents: print(i) """
6. 標簽的其它一些處理方法
# 1. prettify方法 # 這個方法就是在每個標簽后加入一個\n 打印出來是十分規范的h5代碼 一目了然 # 也可以對某個標簽做格式化處理
a = BeautifulSoup(html_str, "lxml").find("a") print(a.prettify()) # 2.contents方法
li = BeautifulSoup(html_str, "lxml") print(li.contents, type(li.contents)) print(li.childrent, type(li.children)) """ li_find.contents 返回的是一個列表 查找的標簽下的子標簽 包括'\n' li_find.children 返回的是一個迭代器, 迭代器的內容與li_find.contents一樣 """