我有以下HTML:
<div id="foo"><span id="bar">BAR</span> hello</div>
我创建了BeautifulSoup
对象:
soup = BeautifulSoup('<div id="foo"><span id="bar">BAR</span> hello</div>')
bar = soup.find(id="bar")
如何使用hello
元素获取bar
文字?
答案 0 :(得分:2)
使用next_sibling
:
>>> from bs4 import BeautifulSoup as bs
>>> data = '<div id="foo"><span id="bar">BAR</span> hello</div>'
>>> soup = bs(data)
>>> bar = soup.find(id="bar")
>>> bar
<span id="bar">BAR</span>
>>> bar.next_sibling.strip()
u'hello'