是否有一个衬里,我可以从 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)