带有beautifulsoup的html元素的文本和元素数据的有序列表

2024-06-06 21:21:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用BeautifulSoup(bs4)解析以下div元素的内容:

<div><!--block-->&nbsp; &nbsp; Some text is here&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; - Another text&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; - More text&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div>

我需要一份有序的内容清单。对于这种情况,清单应包含以下项目:

- non-breaking space
- non-breaking space
- text data
- br
- non-breaking space
...
- non-breaking space

使用tag.find\u全部()我可以得到一个标签列表,比如“br”,但是所有其他数据,比如不间断空格或文本数据,都不会被返回tag.find\u全部(). 你知道吗


Tags: 数据textbrdiv元素内容tagspace