這個問題是從超級用戶遷移過來的,因為它可以在 Stack Overflow 上回答。 2 天前遷移 。
我讀了一本書,內容如下:
from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen('http://www.pythonscraping.com/pages/page3.html')
bs = BeautifulSoup(html, 'html.parser')
for child in bs.find('table',{'id':'giftList'}).children:
print(child)
此代碼列印giftList 表中的產品行串列,包括列標簽的初始行。如果您使用descendants() 函式而不是children() 函式來撰寫它,將在表中找到大約兩打標簽并列印出來,包括img 標簽、span 標簽和單獨的td 標簽。
我測驗了它,在使用 .children 或 .descendants 時,我沒有看到兩個輸出有區別。誰能告訴我在使用 .children 和使用 .descendants 時它會列印什么。
uj5u.com熱心網友回復:
區別在于深度級別。children將達到最大深度的一級。descendants將列印所有內容,每次都達到最大深度。
如果我們從sisters.htmlbeautifulsoup 檔案中摘錄
<p ><b>The Dormouse's story</b></p>
for child in p.children:
print(child)
>>> <b>
for child in p.descendants:
print(child)
>>> <b>
>>> "The Dormouse's story"
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/344492.html
上一篇:bs4不回傳完整的HTML
