python BeautifulSoup怎么获取无标签文本?

<p>aaa</p>bbb
<p>ccc</p>ddd

怎么获取bbb和ddd呢?

阅读 10.8k
2 个回答

可以使用findAll()的text参数。

text is an argument that lets you search for NavigableString objects instead of Tags. Its value can be a string, a regular expression, a list or dictionary, True or None, or a callable that takes a NavigableString object as its argument:

详细请见文档findAll部分:
http://www.crummy.com/software/Beauti...

doc = '<p>aaa</p>bbb<p>ccc</p>ddd'
soup = BeautifulSoup(doc, 'lxml')
for i in soup.find_all('p'):
    print(i.next_sibling)

输出

bbb
ddd
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题