【发布时间】:2015-09-17 08:38:57
【问题描述】:
from BeautifulSoup import BeautifulSoup
html = '''<div class="thisText">
Poem <a href="http://famouspoetsandpoems.com/poets/edgar_allan_poe/poems/18848">The Raven</a>Once upon a midnight dreary, while I pondered, weak and weary... </div>
<div class="thisText">
In the greenest of our valleys By good angels tenanted..., part of<a href="http://famouspoetsandpoems.com/poets/edgar_allan_poe/poems/18848">The Haunted Palace</a>
</div>'''
soup = BeautifulSoup(html)
all_poems = soup.findAll("div", {"class": "thisText"})
for poems in all_poems:
print(poems.text)
我有这个示例代码,但我找不到如何在已删除的标签周围添加空格,因此当<a href...> 中的文本被格式化时,它可以被读取并且不会像这样显示:
诗乌鸦曾经在一个沉闷的午夜,当我沉思时,虚弱而疲倦......
在我们最绿色的山谷中由善良的天使租用......,鬼宫的一部分
【问题讨论】:
-
呃,您的原始 HTML 包含链接文本,这些链接文本与相邻的单词组合在一起。
标签: python html beautifulsoup html-parsing