我正在尝试使用BeautifulSoup转换一大块HTML文本.这是一个例子:
<div>
<p>
Some text
<span>more text</span>
even more text
</p>
<ul>
<li>list item</li>
<li>yet another list item</li>
</ul>
</div>
<p>Some other text</p>
<ul>
<li>list item</li>
<li>yet another list item</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
我尝试过这样的事情:
def parse_text(contents_string)
Newlines = re.compile(r'[\r\n]\s+')
bs = BeautifulSoup.BeautifulSoup(contents_string, convertEntities=BeautifulSoup.BeautifulSoup.HTML_ENTITIES)
txt = bs.getText('\n')
return Newlines.sub('\n', txt)
Run Code Online (Sandbox Code Playgroud)
...但是那样我的span元素总是在新的一行上.这当然是一个简单的例子.有没有办法让HTML页面中的文本在浏览器中呈现的方式(不需要css规则,只有常规的div,span,li等元素呈现)?