小编Jos*_*uce的帖子

BeautifulSoup,get_text(),splitlines(),如何删除pythonic oneliner中的空行?

是否有一个衬里,我可以从 soup 对象获取文本,然后使用 splitlines 获取 html 中每一行的列表。然后删除列表中所有多余的空行,其中只有换行符。

我不想编写另一个 for 循环来传递两次并清理新行。另外,任何其他 pythonic 方式来做到这一点都是值得赞赏的。

soup = BeautifulSoup('myhtml.html', 'html.parser')
sections = soup.findAll(div, class_='section')
lines = []
for section in sections:
    lines = lines + section.get_text().splitlines()
Run Code Online (Sandbox Code Playgroud)

python beautifulsoup

3
推荐指数
1
解决办法
1846
查看次数

标签 统计

beautifulsoup ×1

python ×1