Python 如何使用Beautiful Soup只选择多个表中的一个
我有一个包含4或5个不同表的html文档。我想要的有一个属性class=“data”。我想不出如何让BeautifulSoup只返回那张桌子Python 如何使用Beautiful Soup只选择多个表中的一个,python,html,python-2.7,web-scraping,beautifulsoup,Python,Html,Python 2.7,Web Scraping,Beautifulsoup,我有一个包含4或5个不同表的html文档。我想要的有一个属性class=“data”。我想不出如何让BeautifulSoup只返回那张桌子 soup = BeautifulSoup(myhtml) t = soup.findAll('table', 'class="data"') for table in t: rows = table.findAll('tr') for tr in rows: cols = tr.findAll('td')
soup = BeautifulSoup(myhtml)
t = soup.findAll('table', 'class="data"')
for table in t:
rows = table.findAll('tr')
for tr in rows:
cols = tr.findAll('td')
for td in cols:
print td
如果我删除上面的'class=“data”“,我将从每个表中获得结果。是否可以仅选择带有class=“data”的选项。或者,是否有其他方法可以遍历这些表?将
类
属性指定为字典,如下所示:
t = soup.findAll('table', {'class': 'data'})
如果您使用bs4,您可以使用CSS选择器,使用select
方法:
t = css_soup.select("table.data")
请查看以下代码:
t = soup.find_all('table', class_='data')
属性类需要在下面引用我找不到一个。。。“交易报告”这不起作用。另一个答案(t=soup.findAll('table',{'class':'data'}))解决了这个问题。下划线似乎不起作用。是的,对不起,我弄错了,下划线在另一边。