我正在尝试<p>使用BeautifulSoup 从网页中的元素中删除所有内部html .有内部标签,但我不在乎,我只想获得内部文本.
例如,对于:
<p>Red</p>
<p><i>Blue</i></p>
<p>Yellow</p>
<p>Light <b>green</b></p>
Run Code Online (Sandbox Code Playgroud)
我怎样才能提取:
Red
Blue
Yellow
Light green
Run Code Online (Sandbox Code Playgroud)
我既不需.string也不.contents[0]需要.也不是.extract(),因为我不想提前指定内部标签 - 我想处理任何可能发生的事情.
BeautifulSoup中是否有'just get the visible HTML'类型的方法?
---- ------ UPDATE
在建议上,尝试:
soup = BeautifulSoup(open("test.html"))
p_tags = soup.findAll('p',text=True)
for i, p_tag in enumerate(p_tags):
print str(i) + p_tag
Run Code Online (Sandbox Code Playgroud)
但这没有帮助 - 它打印出来:
0Red
1
2Blue
3
4Yellow
5
6Light
7green
8
Run Code Online (Sandbox Code Playgroud)