【发布时间】:2019-06-29 20:51:36
【问题描述】:
我对 Python、MySQL 和 API 开发非常陌生。因此,我创建了一个使用邮递员安排日历的简单请求(经过数小时的在线研究)。我能够在本地主机上创建一个 MySQL 数据库。我能够使用 Python 插入数据,但我不想手动存储 API 响应。请查看我的代码,如果有任何机构可以帮助我,我将不胜感激。
使用 Python 请求调用
import requests
url = "https://shedul.com/api/v1/appointments"
querystring = {"minDate":"02/01/2019","maxDate":"02/08/2019"}
payload = ""
headers = {
'Authorization': "Basic ************************"}
response = requests.request("GET", url, data=payload, headers=headers, params=querystring)
print(response.json())
我会得到类似这样的响应(我会有很多请求和更多字段,但我需要这几个字段),
[
{
"id": 2649,
"firstName": "Walikada",
"lastName": "Sumana",
"phone": "7894561452",
"email": "wsumanar@gmail.com",
"date": "February 8, 2019",
"time": "2:00pm",
"endTime": "3:00pm",
"dateCreated": "February 5, 2019",
"datetimeCreated": "2019-02-05T15:57:13-0600",
"datetime": "2019-02-08T14:00:00-0700",
},
{
"id": 264693950,
"firstName": "Wade",
"lastName": "Gulikapupusa",
"phone": "789691985",
"email": "W.Gulika77@yhaoo.com",
"date": "February 8, 2019",
"time": "2:00pm",
"endTime": "2:20pm",
"dateCreated": "February 4, 2019",
"datetimeCreated": "2019-02-04T15:05:07-0600",
"datetime": "2019-02-08T14:00:00-0600",
}
]
我发现的连接 MySQl 数据库的代码,
import mysql.connector
connection = mysql.connector.connect(
host="localhost",
user="root",
passwd="admin",
database="techbrandtest"
)
mycursor = connection.cursor()
sqlcode = """INSERT INTO techbrandtest.acuity
(
id,
firstName,
lastName,
phone,
email,
date,
time,
endTime,
dateCreated,
datetimeCreated,
datetime
)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"""
insertvalues = ('546606', 'Suneth', 'Tharinda', '8016381256', 'wimalasena@gmail.com', '2019-02-02', '', '','2019-02-02','2019-02-02 23:59:59', '2019-02-05 23:59:59')
mycursor.execute(sqlcode, insertvalues)
connection.commit()
print(mycursor.rowcount, "was inserted.")
我想让它成为一个代码并将每个响应存储在 MySQL 数据库中。我不确定这要求太多,但我们将不胜感激。
非常感谢您的宝贵时间
【问题讨论】:
标签: python mysql python-3.x api mysql-python