Python读URL数据写入MySQL数据库
发布网友
发布时间:2022-04-22 20:46
我来回答
共1个回答
热心网友
时间:2022-04-06 05:30
import mysql.connection
import requests
import time
conn = mysql.connector.connect(user='root', password='password', database='test')
cursor=conn.cursor()
cursor.execute('create table json(id varchar(20) primary key, text varchar(20))')
conn.commit()
url='http://f.apiplus.net/cqssc.json'
n=0
while Ture:
n=n+1
req=requests.get(url).json()
cursor.execute('insert into user (id, text) values (%s, %s)', [n, req])
conn.commit()
time.sleep(60)
追问req = requests.get(url).json()
cursor.execute('insert into user (id, text) values (%s, %s)', [n,req])
报错:TypeError: sequence item 0: expected str instance, dict found
追答req = requests.get(url).json()
换成:req = requests.get(url).text