python写post请求批量在mysql里面导入data参数
要批量导入data参数到MySQL数据库中,可以使用Python的requests库进行POST请求,并使用MySQL的Python驱动程序(如pymysql)来连接和插入数据。
以下是一个示例代码:
import requests
import pymysql
# 创建MySQL连接
conn = pymysql.connect(host='localhost', user='your_username', password='your_password', db='your_database')
cursor = conn.cursor()
# 批量导入数据
data = [
{'name': 'John', 'age': 25},
{'name': 'Alice', 'age': 30},
{'name': 'Bob', 'age': 35}
]
for item in data:
# 发送POST请求
response = requests.post('http://your_url', data=item)
# 解析响应数据
result = response.json()
# 插入数据到MySQL
if result['success']:
sql = "INSERT INTO your_table (name, age) VALUES (%s, %s)"
cursor.execute(sql, (item['name'], item['age']))
conn.commit()
# 关闭连接
cursor.close()
conn.close()
请根据你的实际情况,将代码中的your_username、your_password、your_database、your_table和your_url替换为正确的值
原文地址: http://www.cveoy.top/t/topic/hSgG 著作权归作者所有。请勿转载和采集!