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()


©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容