可以使用Python中的requests库发送POST请求,并使用MySQLdb或pymysql库将接收到的json数据写入MySQL数据库。

以下是一个示例代码:

import requests
import json
import MySQLdb

# 发送POST请求获取json数据
url = 'https://example.com/api'
data = {'key1': 'value1', 'key2': 'value2'}
response = requests.post(url, data=json.dumps(data))

# 将json数据解析为Python对象
json_data = json.loads(response.text)

# 连接MySQL数据库
db = MySQLdb.connect(host='localhost', user='user', passwd='password', db='database')
cursor = db.cursor()

# 从json数据中获取需要写入数据库的字段值
field1 = json_data['field1']
field2 = json_data['field2']
field3 = json_data['field3']

# 使用SQL语句将数据写入MySQL数据库
sql = "INSERT INTO table_name (field1, field2, field3) VALUES (%s, %s, %s)"
values = (field1, field2, field3)
cursor.execute(sql, values)
db.commit()

# 关闭数据库连接
db.close()

需要根据实际情况修改代码中的url、data、host、user、passwd、db、table_name、field1、field2和field3等参数

怎么把发出POST请求接受的json数据通过Python写入MySQL数据库

原文地址: https://www.cveoy.top/t/topic/eMHc 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录