Python读URL数据写入MySQL数据库
编写Pthon3.5语句,读取API接口(URL='http://f.apiplus.net/cqssc.json')的内容,写入MySQL5.7数据表(表结构自拟)中,...
编写Pthon3.5语句,读取API接口(URL= 'http://f.apiplus.net/cqssc.json')的内容,写入MySQL5.7数据表(表结构自拟)中,定时每分钟执行一次。
展开
2018-07-17
展开全部
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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询