Nic*_*ick 31 python parsing beautifulsoup
我试图用Python-> Beautiful Soup解析一个看起来像这样的网页:
我试图提取突出显示的td div的内容.目前我可以得到所有的div
alltd = soup.findAll('td')
for td in alltd:
print td
Run Code Online (Sandbox Code Playgroud)
但是我试图缩小范围以搜索"tablebox"类中的tds,它仍然可能会返回30+但是更容易管理的数字超过300+.
如何提取上图中突出显示的td的内容?
Bo *_*ich 55
知道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
Run Code Online (Sandbox Code Playgroud)
这将打印所有td标签的所有文本,其中"align-right"类具有父类div和"tablebox"类.