import requests
import json
import pymysql
import time
# 金士頓(Kingston) 游戲電競耳機(jī)頭戴式 電腦耳機(jī) 商品分析
# 先將此商品的用戶評價明細(xì)保存進(jìn)庫,再將這些商品評價的分類匯總統(tǒng)計情況保存進(jìn)庫嫉柴。
conn = pymysql.connect(host='localhost', user='root', passwd='123456', db='world', port=3306, charset='utf8')
cursor = conn.cursor()
headers = {'Referer':'https://item.jd.com/6419264.html',
? ? 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'
}
urls = ['https://sclub.jd.com/comment/productPageComments.action?productId=6419264&score=0&sortType=5&page={}&pageSize=10'.format(str(i))for iin range(0,7)]
for url in urls:
? ? ?res = requests.get(url,headers=headers)
? ? ?json_data = json.loads(res.text)
? ? ?comments = json_data['comments']
? ? ?for comment in comments:
? ? ? ? ? content = comment['content']
? ? ? ? ? creationTime = comment['creationTime']
? ? ? ? ? nickname = comment['nickname']
? ? ? ? ? productColor = comment['productColor']
? ? ? ? ? # print(content,creationTime,nickname,productColor)
? ? ? ? ? cursor.execute("insert into goodsinf(content,creationTime,nickname,productColor)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? values(%s,%s,%s,%s)", (content,creationTime,nickname,productColor))
? ? ? ? ? conn.commit()
? ? ?time.sleep(4)
cursor = conn.cursor()
for url in urls:
? ? ?res = requests.get(url, headers=headers)
? ? ?json_data = json.loads(res.text)
? ? ?hotCommentTagStatistics = json_data['hotCommentTagStatistics']
? ? ?for Statistic in hotCommentTagStatistics:
? ? ? ? ? ?count = Statistic['count']
? ? ? ? ? ?name = Statistic['name']
? ? ? ? ? ?# print(name,count)
? ? ? ? ? ?cursor.execute("insert into goodsfx(lxname,lxcount) values(%s,%s)",(name,count))
? ? ? ? ? ?conn.commit()
? ? ? ?time.sleep(4)
conn.close()