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
<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
,而不是&LT; TD&GT;
。此外 nextSibling
将寻找下一个 NavigableString
的或的标记
因此,即使你有&LT; TD方式&gt;
它不会工作,你所期望的方式
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]
其实你可以只是做
Actually you could just do
print soup.find(text="Address:").findNext('td').contents[0]
由于 FindNext中
只是调用接下来
一遍又一遍,而接下来
查找下一个元素的为解析的反复,直到它匹配。
Since findNext
just calls next
over and over again, and next
finds the next element as parsed repeatedly until it matches.
这篇关于Beautifulsoup - nextSibling的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!