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 = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36',
'Referer':'https://item.jd.com/7765111.html'
}
urls = ['https://sclub.jd.com/comment/productPageComments.action?productId=7765111&score=0&sortType=5&page={}&pageSize=10'.format(str(i))for iin range(0,100)]
for url in urls:
res = requests.get(url,headers=headers)
json_data = json.loads(res.text)
comments = json_data['comments']
for commentin comments:
content = comment['content']
creationTime = comment['creationTime']
nickname = comment['nickname']
productColor = comment['productColor']
cursor.execute("insert into Lenovo (content,creationTime,nickname,productColor) values(%s,%s,%s,%s)",(content,creationTime,nickname,productColor))
conn.commit()
time.sleep(4)
conn.close()