使用Beautifulsoup浏览表格时遇到问题


<div class ="table">
<table class="stats">
<td>Not this</td>
</table>
<table class="stats">
<td>I want this</td>
</table>
</div>

containers = page_soup.findAll("table", {"class":"stats"})
container = containers[0]
rows = container.findChildren(['td'])

我只得到第一行文本,但我想要第二行,它似乎工作不正常。。。请帮忙,谢谢!

如果使用[0],则只得到第一个元素。使用[1]获取第二个元素

或者使用for-循环来处理所有元素


text = '''<div class ="table">
<table class="stats">
<td>Not this</td>
</table>
<table class="stats">
<td>I want this</td>
</table>
</div>'''

from bs4 import BeautifulSoup as BS
soup = BS(text, 'html.parser')
containers = soup.findAll("table", {"class":"stats"})
container = containers[0]
rows = container.findChildren(['td'])
print('1st:', rows)
container = containers[1]
rows = container.findChildren(['td'])
print('2nd:', rows)
print('--- for-loop ---')
for container in containers:
print(container.findChildren(['td']))
print('-')

结果

1st: [<td>Not this</td>]
2nd: [<td>I want this</td>]
--- for-loop ---
[<td>Not this</td>]
-
[<td>I want this</td>]
-

最新更新