我正在尝试用BeautifulSoup转换一大堆HTML文本。这里是一个例子:
Some text
more text
even more text
- list item
- yet another list item
Some other text
- list item
- yet another list item
我试着做一些像:
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)
…但是我的span元素总是在新行上。这当然是一个简单的例子。有没有办法在HTML页面中获取文本,就像在浏览器中呈现的方式一样(不需要CSS规则,只是渲染div,span,li等元素的常规方式)在Python中?