解析京东评论到数据库中:0

import requests
import json
import pymysql

def jd_data(cursor):
    url = 'https://club.jd.com/comment/productPageComments.action?productId=100026667884&score=0&sortType=5&page=0&pageSize=10&isShadowSku=0&fold=1'
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36'
    }
    r = requests.get(url=url, headers=headers)
    # print(r.text)
    json_dict = json.loads(r.text)
    json_list = json_dict['comments']
    print(json_list)
    num=0
    for data in json_list:
        # id
        num+=1
        # 定义一个sql语句提交 ,需要两个数值
        sql_add = 'insert into jd_da values (%s,%s)'
        # 将num和data['content']传入sql_add
        cursor.execute(sql_add,(num,data['content']))
        # 提交到定义的conn数据库
        conn.commit()
    # 接口关闭
    cursor.close()
    conn.close()

if __name__ == '__main__':
    conn = pymysql.connect(host='这里填ip',port=3306,user='root',passwd='S7865324.',db='test',charset='utf8')
    cursor = conn.cursor()
    jd_data(cursor)

从数据库中获取数据:

import pymysql

# 数据库配置
conn = pymysql.connect(host='81.68.148.230', port=3306, user='root', passwd='S7865324.', db='test', charset='utf8')
# 游标
cursor = conn.cursor()
# sql语句
sql_get = 'select * from jd_da'
# 运行sql
cursor.execute(sql_get)
# 将运行sql语句的返回的数据接收到res
res = cursor.fetchall()
# print(res)  # 返回的是一个源数据组
for i in res:
    print('第', list(i)[0])
    print(list(i)[1])
    print('==========')
conn.commit()  # 提交
cursor.close()
conn.close()
最后修改:2022 年 08 月 31 日
如果觉得我的文章对你有用,请随意赞赏