如何用beauthoulsoup提取评论?

2024-06-07 00:56:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我对python和数据挖掘还不熟悉,所以我有一个关于从输出中提取部分的问题。我在3.6中使用Python,今天早上已经更新了所有的东西。我已经匿名输出并删除了包含密码、令牌等的所有行。在

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。别这样。在

提前谢谢。在


Tags: fromdiv数据挖掘密码datahtmlcommentstackoverflow
1条回答
网友
1楼 · 发布于 2024-06-07 00:56:22

试试这个:

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)

输出:

^{pr2}$

相关问题 更多 >