csdn_spider/upload_file.py

46 lines
1.3 KiB
Python
Raw Normal View History

2024-11-16 19:09:14 +00:00
#!/usr/bin/env python
# coding: utf-8
import json
import re
import os
import requests
def upload_file(file_path):
# Open a file in read mode
file = open(file_path, 'r', encoding='utf-8')
path = os.path.split(file_path)
# Read the entire content of the file
content = file.read()
print(content,path)
image_arr = re.findall(r'(?:!\[(.*?)\]\(.*?\))',content)#提最述与rul
for i in image_arr:
print(path[0] + "\\" + i)
files = {'image': open( path[0] + "\\" + i, 'rb')}
response1 = requests.post('https://www.testingcloud.club/sapi/api/image_upload',
files=files,timeout=30)
if response1.status_code==200:
if(response1.text != ''):
jsons = json.loads(response1.text)
print(jsons)
content = content.replace(i, 'https://www.testingcloud.club/sapi/api/image_download/%s'%jsons['url'])
requests.put("https://www.testingcloud.club/sapi/api/article_tree",json.dumps({
"title": (path[1]),
"content":(content),
"spend_time":1,
"father":2500,
"level":1,
"is_public":1,
"author":"admin"
}))
# Close the file
file.close()
# Read the entire content of the file
if __name__ == "__main__":
# Open a file in read mode
upload_file("F:\\dd\\dd.md")