第三讲作业

import requests

import json

import pymysql

import time

# 金士顿(Kingston) 游戏电竞耳机头戴式 电脑耳机 商品分析

# 先将此商品的用户评价明细保存进库,再将这些商品评价的分类汇总统计情况保存进库。

conn = pymysql.connect(host='localhost', user='root', passwd='123456', db='world', port=3306, charset='utf8')

cursor = conn.cursor()

headers = {'Referer':'https://item.jd.com/6419264.html',

    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'

}

urls = ['https://sclub.jd.com/comment/productPageComments.action?productId=6419264&score=0&sortType=5&page={}&pageSize=10'.format(str(i))for iin range(0,7)]

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 goodsinf(content,creationTime,nickname,productColor)                                  values(%s,%s,%s,%s)", (content,creationTime,nickname,productColor))

          conn.commit()

     time.sleep(4)

cursor = conn.cursor()

for url in urls:

     res = requests.get(url, headers=headers)

     json_data = json.loads(res.text)

     hotCommentTagStatistics = json_data['hotCommentTagStatistics']

     for Statistic in hotCommentTagStatistics:

           count = Statistic['count']

           name = Statistic['name']

           # print(name,count)

           cursor.execute("insert into goodsfx(lxname,lxcount) values(%s,%s)",(name,count))

           conn.commit()

       time.sleep(4)

conn.close()

你可能感兴趣的:(第三讲作业)