Python作业3:对京东商城格力空调爬取数据,存入MySQL数据库

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/4856816.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=4856816&score=0&sortType=5&page={}&pageSize=10'.format(str(i))for iin range(0,3)]

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']

              referenceName = comment['referenceName']

              # print(content,creationTime,nickname,referenceName)

              cursor.execute("insert into aircondioner_info (content,creationTime,nickname,referenceName) values(%s,%s,%s,%s)",

                        (content,creationTime,nickname,referenceName))

               conn.commit()

      time.sleep(4)

conn.close()


你可能感兴趣的:(Python作业3:对京东商城格力空调爬取数据,存入MySQL数据库)