我有下表:
<table class="information">
<tr> .... lots of rows with <th> and <td></tr>
<tr>
<th>Nationality</th>
<td><a href="..">Stackoverflowian</a></td>
</tr>
</table>
我想在th的td标签内找到带有“国籍”的文本。我应该如何导航?我正在使用Beautifulsoup和Python。
补充说,在此之上还有许多th和td标签,以强调仅仅找到第一个th还不够。
找到th
标签,然后获取下一个同级标签:
soup = BeautifulSoup(html)
ths = soup.find_all('th')
for th in ths:
if th.text == "Nationality":
print th.next_sibling.next_sibling.text
# Stackoverflowian
我们需要做next_sibling
两次,因为第一个将给换行符。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句