![](/img/trans.png)
[英]Python/Beautiful Soup find particular heading output full div
[英]Beautiful Soup find children for particular div
我試圖用 Python->Beautiful Soup 解析一個看起來像這樣的網頁:
我正在嘗試提取突出顯示的 td div 的內容。 目前我可以通過
alltd = soup.findAll('td')
for td in alltd:
print td
但我試圖縮小范圍以搜索“tablebox”類中的 tds,它仍然可能返回 30+,但比 300+ 更易於管理。
如何提取上圖中突出顯示的 td 的內容?
知道 BeautifulSoup 在一個元素中找到的任何元素仍然具有與該父元素相同的類型是很有用的——也就是說,可以調用各種方法。
因此,對於您的示例來說,這是一些有效的代碼:
soup = BeautifulSoup(html)
divTag = soup.find_all("div", {"class": "tablebox"})
for tag in divTag:
tdTags = tag.find_all("td", {"class": "align-right"})
for tag in tdTags:
print tag.text
這將打印所有具有“align-right”類的td
標簽的所有文本,這些標簽的父div
為“tablebox”類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.