admin 管理员组

文章数量: 1086865

Python request爬取华为P30手机京东评论

最近准备把多年的手机升级,瞅准了华为p30系列,于是就想瞅瞅评论看看,以下为源码:

import requests
import json
import re
import timecount = 0
for i in range(30):count += 1url = '.action?'string = 'productId=100004323294&score=0&sortType=5&page={}&pageSize=10&isShadowSku=0&fold=1'.format(i)param = {i.split('=')[0]: i.split('=')[1]for i in string.split('&')} # 列表推导式headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36','Referer': '.html'}r = requests.get(url, params=param, headers=headers)t = json.loads(r.text) # 将json字符还原成列表time.sleep(1)for j in range(len(t['comments'])):creationTime = t['comments'][j]['creationTime']comments = t['comments'][j]['content']comments = re.sub('\\n|\\r|\\t| ', '', comments) # 取出列表中元素并格式化with open('comments.txt', 'a', encoding='utf-8') as file:file.write(comments + '\n') # 写入文件print('\r当前进度为{:.2%}'.format(count / 30), end='')
print('\nProcess done!')
import wordcloud
import oswith open('comments.txt', 'r', encoding='utf-8') as file:f = file.read()wordmap = wordcloud.WordCloud(width=1000, height=400,margin=2)wordmap.generate(f)wordmap.to_file('wm.png')os.system('wm.png')

出来图片:

完工!!

本文标签: Python request爬取华为P30手机京东评论