bs4之标签树的下行遍历
生活随笔
收集整理的這篇文章主要介紹了
bs4之标签树的下行遍历
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import requests
from bs4 import BeautifulSoupdef bianliSoup(url):r = requests.get(url, timeout=30)r.raise_for_status()r.encoding = r.apparent_encodingdemo = r.textsoup = BeautifulSoup(demo, "html.parser")# 子節點列表,contents為列表類型print("子節點列表:")print(soup.head.contents) # 輸出head標簽的子節點內容print(soup.body.contents) # 輸出body標簽的子節點內容print(len(soup.body.contents)) # 輸出body標簽的子節點數量print(soup.body.contents[1]) # 輸出第一個子節點內容# 兒子節點列表,childern和descendants為迭代類型,只能用在for循環之中print("遍歷兒子節點:") # 與contents類似,區別是children為遍歷兒子節點for child in soup.body.childern:print(child)# 子孫節點列表print("子孫節點列表:") # 包含所有子孫節點列表for child in soup.body.descendants:print(child)url = "https://python123.io/ws/demo.html"
bianliSoup(url)
?
總結
以上是生活随笔為你收集整理的bs4之标签树的下行遍历的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: requests库之IP归属地查询
- 下一篇: bs4之标签树的平行遍历