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; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0',
? ? 'referer': 'https://item.jd.com/100005270438.html'
}
urls = ['https://sclub.jd.com/comment/productPageComments.action?productId=100005270438&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']
? ? ? ? cursor.execute("insert into jd_info (content,creationTime,nickname,productColor) values(%s,%s,%s,%s)",
? ? ? ? ? ? ? ? ? ? ? (content, creationTime, nickname, productColor))
? ? ? ? conn.commit()
? ? time.sleep(4)
conn.close()