美丽汤为特定的div找到孩子 [英] Beautiful Soup find children for particular div

查看:85
本文介绍了美丽汤为特定的div找到孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python-> Beautiful Soup解析如下所示的网页:

我正在尝试提取突出显示的td div的内容.目前,我可以通过

获取所有的div

alltd = 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆