Beautifulsoup - nextSibling [英] 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?
推荐答案
问题是您找到的是 The problem is that you have found a 这就是你想要的: 或者更简洁: 其实你可以这样做 由于 Since 这篇关于Beautifulsoup - nextSibling的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!NavigableString
,而不是 .另外 nextSibling
会找到下一个 NavigableString
或 Tag
所以即使你有 它不会像你期望的那样工作.
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 的下一个元素直到匹配为止.findNext
just calls next
over and over again, and next
finds the next element as parsed repeatedly until it matches.
登录
关闭