【发布时间】:2016-03-13 23:12:42
【问题描述】:
星期天快乐!
免责声明:我对 python 很陌生(只有两周的学习时间)。
好吧,所以我使用 beautifulsoup 构建了一小段代码,用于从 tripadvisor 页面中删除评论内容并将其保存到文本文件中。
我的问题是,当我打印结果时,所有评论都会出现。但是,当我尝试将其保存到本地文本文件时,只会保存第一个评论。
这是我到目前为止的一段代码:
#prompt for URL of the page to scrap
print " Paste url here"
importurl = raw_input()
print " Import of:"
print " %s " %importurl
#convert the page into a soup
r = requests.get(importurl)
soup = BeautifulSoup(r.content, "lxml")
#look for the partial entry of the review
resultsoup = soup.find_all("p", {"class" : "partial_entry"})
#save the reviews to a test text file locally
for review in resultsoup:
review_list = review.get_text()
print review_list
with open('testreview.txt', 'w') as fid:
fid.write(unidecode(review_list))
【问题讨论】:
-
什么是
new_entry? -
很好发现,损坏的代码!它应该是 review_list!
标签: python beautifulsoup