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)
Run Code Online (Sandbox Code Playgroud)
我有这个示例代码,但我找不到如何在删除的标签周围添加空格,因此当<a href...>格式化内部的文本时,它可以是可读的,并且不会像这样显示:
诗乌鸦从前沉闷的午夜,而我沉思,虚弱和疲倦......
在我们山谷中最绿的地方 由好天使租住......,鬼宫的一部分