美丽汤为特定的div找到孩子 [英] Beautiful Soup find children for particular div
问题描述
我正在尝试使用Python-> Beautiful Soup解析如下所示的网页:
我正在尝试提取突出显示的td div的内容.目前,我可以通过
获取所有的divalltd = soup.findAll('td')
for td in alltd:
print td
但是我试图缩小搜索范围,以在类"tablebox"中搜索tds.仍可能返回30+,但比300+更易于管理.
如何提取上图中突出显示的td的内容?
了解到BeautifulSoup在一个元素中找到的任何元素仍然具有与父元素相同的类型是很有用的,也就是说,可以调用各种方法./p>
所以这是您示例的有效代码:
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
这将打印所有td
标签的所有文本,其类别为"align-right".的父级div
属于"tablebox"类.
I have am trying to parse a webpage that looks like this with Python->Beautiful Soup:
I am trying to extract the contents of the highlighted td div. Currently I can get all the divs by
alltd = soup.findAll('td')
for td in alltd:
print td
But I am trying to narrow the scope of that to search the tds in the class "tablebox" which still will probably return 30+ but is more managable a number than 300+.
How can I extract the contents of the highlighted td in picture above?
It is useful to know that whatever elements BeautifulSoup finds within one element still have the same type as that parent element - that is, various methods can be called.
So this is somewhat working code for your example:
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
This will print all the text of all the td
tags with the class of "align-right" that have a parent div
with the class of "tablebox".
这篇关于美丽汤为特定的div找到孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!