?一文告訴你蓬萊閣到底如何?
- 一、前言
- 二、環境準備
- 三、具體實作
- 1、短評游客評論并保存
- 2、詞云制作
- 3、成果
- 四、最后
完成目標:
??獲取評論并制作詞云
一、前言
??畢竟會在煙臺待上三年,先了解了解煙臺這個地方,
二、環境準備
編輯器:pycharm
用到的庫:requests、wordcloud、jieba
三、具體實作
1、短評游客評論并保存
??爬取去哪兒網關于蓬萊島的游客評論的前20頁
def save_comment():
fp = open("comment.txt", mode="w", encoding="utf-8")
for num in range(1, 20):
url = 'https://piao.qunar.com/ticket/detailLight/sightCommentList.json?sightId=3827&index=' + str(
num) + '&page=' + str(num) + '&pageSize=10&tagType=0'
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36 Edg/93.0.961.38',
"accept": "application/json, text/javascript, */*; q=0.01",
}
response = requests.get(url=url, headers=headers)
try:
commentList = response.json()['data']['commentList']
for comment in commentList:
# content.append()
fp.write(comment['content'] + "\n")
except Exception as e:
pass
fp.close()
2、詞云制作
??讀取評論并制作詞云
def stopwordslist(): # 停用詞串列
stopwords = [line.strip() for line in open('Chinesestopword.txt', encoding='UTF-8').readlines()]
return stopwords
def get_wcd():
fp = open("comment.txt", "r", encoding="utf-8")
data = fp.read()
stopwords=stopwordslist()
data_list = jieba.lcut(data)
data_list = " ".join(data_list)
outstr = ''
for word in data_list:
if word not in stopwords:
if word != '\t':
outstr += word
outstr += " "
wcd = wordcloud.WordCloud(
font_path="simkai.ttf"
, colormap="brg"
, width=800
, height=400
, max_words=200
, background_color="white"
, scale=16
).generate(outstr)
wcd.to_file('comment.jpg')
3、成果

四、最后
??還是不錯滴,有機會去~~~
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/298376.html
標籤:python
上一篇:關于這個大一結束——————
下一篇:Python學習總結
