import requests
import json
import pymysql
import time
conn = pymysql.connect(host='localhost', user='root', passwd='123456', db='mydb', port=3306, charset='utf8')
#连接数据库
cursor = conn.cursor()
headers = {
'Referer':'https://item.jd.com/100000525219.html',
'User-Agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Mobile Safari/537.36'
}
urls = ['https://sclub.jd.com/comment/productPageComments.action?productId=100000525219&score=0&sortType=5&page={}&pageSize=10'.format(str(i)) for i in range(0,10)]
for url in urls:
# print(url)
res = requests.get(url,headers=headers)
# print(res.text)
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']
productSize = comment['productSize']
#评分
score = comment['score']
# print(content,creationTime,nickname,productColor,productSize,score)
cursor.execute("insert into goods_sellinfo (goods_id,content,creationTime,nickname,productColor,productSize,score) values(%s,%s,%s,%s,%s,%s,%s)", ('100000525219',content,creationTime,nickname,productColor,productSize,score))
conn.commit()
time.sleep(4)
select_sql = "SELECT avg(score),max(score),min(score) from kindle_paperwhite4 group by goods_id"
cursor.execute(select_sql)
result = cursor.fetchall()
cursor.close()
conn.close()
print(result)