[英]How to extract a comment with beautifulsoup?
我是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&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&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.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.