Beautifulsoup - nextSibling [英] Beautifulsoup - nextSibling

查看:14
本文介绍了Beautifulsoup - nextSibling的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下内容获取内容我的家庭住址",但出现了 AttributeError:

I'm trying to get the content "My home address" using the following but got the AttributeError:

address = soup.find(text="Address:")
print address.nextSibling

这是我的 HTML:

<td><b>Address:</b></td>
<td>My home address</td>

向下导航 td 标签并拉取内容的好方法是什么?

What is a good way to navigate down td tag and pull the content?

推荐答案

问题是您找到的是 NavigableString,而不是 .另外 nextSibling 会找到下一个 NavigableString Tag 所以即使你有 它不会像你期望的那样工作.

The problem is that you have found a NavigableString, not the <td>. Also nextSibling will find the next NavigableString or Tag so even if you had the <td> it wouldn't work the way you expect.

这就是你想要的:

address = soup.find(text="Address:")
b_tag = address.parent
td_tag = b_tag.parent
next_td_tag = td_tag.findNext('td')
print next_td_tag.contents[0]

或者更简洁:

print soup.find(text="Address:").parent.parent.findNext('td').contents[0]

其实你可以这样做

print soup.find(text="Address:").findNext('td').contents[0]

由于 findNext 只是一遍又一遍地调用 next,并且 next 会反复查找 as parsed 的下一个元素直到匹配为止.

Since findNext just calls next over and over again, and next finds the next element as parsed repeatedly until it matches.

这篇关于Beautifulsoup - nextSibling的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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