#京东商城1加7pro的商品评价,分析得出买'曜岩灰'颜色的人较多
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/71.0.3573.0 Safari/537.36'
, 'Referer':'https://item.jd.com/100005502446.html',
}
urls = [
'https://sclub.jd.com/comment/productPageComments.action?productId=100005502446&score=0&sortType=5&page={}&pageSize=10'.format(
str(i))for iin range(0, 100)]
for urlin 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']
# print(content,creationTime,nickname,productColor)
cursor.execute("insert into 1plus7_info (content,creationTime,nickname,productColor) values(%s,%s,%s,%s)",
(content, creationTime, nickname, productColor))
conn.commit()
time.sleep(4)
conn.close()