# 获取荣耀20手机评论信息
import requests
import json
import pymysql
import time
conn = pymysql.connect(host='localhost', user='root', passwd='123456', db='mysql', port=3306, charset='utf8')
cursor = conn.cursor()
headers = {'Referer':'https://item.jd.com/100003395445.html',
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
}
urls = ['https://sclub.jd.com/comment/productPageComments.action?productId=100003395445&score=0&sortType=5&page={}&pageSize=10'.format(str(i)) for i in range(0,100)]
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 honor_info (content,creationTime,nickname,productColor) values(%s,%s,%s,%s)",
(content,creationTime,nickname,productColor))
conn.commit()
time.sleep(4)
conn.close()