BeautifulSoup:获取内容[]作为单个字符串
有人知道一种优雅的方法来将 soup 对象的全部内容作为单个字符串获取吗?
目前我正在获取 contents
,这当然是一个列表,然后迭代它:
notices = soup.find("div", {"class" : "middlecontent"})
con = ""
for content in notices.contents:
con += str(content)
print con
谢谢!
Anyone know an elegant way to get the entire contents of a soup object as a single string?
At the moment I'm getting contents
, which is of course a list, and then iterating over it:
notices = soup.find("div", {"class" : "middlecontent"})
con = ""
for content in notices.contents:
con += str(content)
print con
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
contents = str(notices)
怎么样?或者
contents = notifications.renderContents()
,这将隐藏 div 标签。What about
contents = str(notices)
?Or maybe
contents = notices.renderContents()
, which will hide the div tag.您可以使用 join() 方法:
或者使用生成器表达式:
You can use the join() method:
Or, using a generator expression:
但该列表是递归的,所以......
我认为这会起作用。
我是 python 新手,所以代码可能看起来有点奇怪
But the list is recursive, so...
I think this will work.
I'm new to python, so the code may look a little weird