import os
import requests
import json
# 指定文件夹路径和分类
folder_path = r'C:\Users\lenovo1\PycharmProjects\pythonProject\pic'
category = '' # 替换为实际的类别值
# 遍历文件夹中的图片文件
for file_name in os.listdir(folder_path):
if file_name.lower().endswith(('jpg', 'jpeg', 'png', 'gif')):
# 构建上传请求的URL和数据
upload_url = 'https://resource.dhfjy.com/index/index/upload'
file_path = os.path.join(folder_path, file_name)
# 打开文件并构建文件对象
with open(file_path, 'rb') as f:
files = {'file': (file_name, f, 'image/png')}
data = {'category': category}
try:
# 发送上传请求
response = requests.post(upload_url, files=files, data=data)
# 检查上传结果并打印响应内容
if response.status_code == 200:
print(f"文件上传成功:{file_name}")
print(response.text)
# 解析JSON格式的响应文本
response_data = json.loads(response.text)
# 提取"data"字段中的"url"值
url_value = response_data['data']['url']
# 提取"data"字段中的"url"值
if 'data' in response_data and 'url' in response_data['data']:
url_value = response_data['data']['url']
# 进行后续操作
data = {
'cover': url_value,
'class_id': '19'
}
# 发送POST请求
response = requests.post('https://resource.dhfjy.com/index/index/addfa', data=data)
# 检查响应状态码并打印响应内容
if response.status_code == 200:
print(response.text)
else:
print(f"POST请求失败:{response.status_code}")
print(url_value)
else:
print("无法提取URL地址")
print(url_value)
else:
print(f"文件上传失败:{file_name}")
except requests.RequestException as e:
print(f"文件上传出现异常:{str(e)}")
本文共 个字数,平均阅读时长 ≈ 分钟,您已阅读:0时0分0秒。
649494848