如何与Beautifulsoup提取评论



我是Python的新手和数据挖掘,因此我有一个关于从输出中提取零件的问题。我在3.6中使用Python,并在今天早上更新了所有内容。我已经匿名化了Ouput,并删除了所有包含密码,令牌等的行。

from bs4 import BeautifulSoup
soup = BeautifulSoup(open("facebookoutput.html"), "html.parser")
comments = soup.findAll('div', class_="_2b06")
print(comments[0]) # show print of first entry:
<div class="_2b06"><div class="_2b05"><a href="/stuartd?fref=nf&amp;rc=p&    amp;__tn__=R-R">some Name </a></div><div data-commentid="100000000000000000222222000000000000000" data-sigil="comment-body">There is nice comment. I like stackoverflow. </div></div>

我坚持要得到`很棒的评论。我喜欢stackoverflow。

预先感谢。

尝试以下:

from bs4 import BeautifulSoup
content="""
<div class="_2b06"><div class="_2b05"><a href="/stuartd?fref=nf&amp;rc=p&    amp;__tn__=R-R">some Name </a></div><div data-commentid="100000000000000000222222000000000000000" data-sigil="comment-body">There is nice comment. I like stackoverflow. </div></div>
"""
soup = BeautifulSoup(content, "html.parser")
comments = ' '.join([item.text for item in soup.select("[data-sigil='comment-body']")])
print(comments)

输出:

There is nice comment. I like stackoverflow.

最新更新