有时候去写爬虫,发现那个请求头是真的多,而且格式比较的复杂,如下图
我这里写了一个一键格式化的代码:
import re
headerStr = '''
浏览器中的请求头复制到这里
'''
ret = ""
for i in headerStr:
if i == '\n':
i = "',\n'"
ret += i
ret = re.sub(": ", "': '", ret)
print(ret[3: -3])
只需要运行一下,就会发现奇迹:
Postman的方式:
先复制网站curl
直接粘贴
直接变成python
postman上传文件
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/upload', methods=['POST'])
def index():
# 使用request模块接收带对应标签的文件列表,这里对应图片和视频
image_files = request.files.getlist('image')
video_files = request.files.getlist('video')
# 判断是否有空文件
if not image_files and not video_files:
return jsonify({
"code": -1,
"message": "No upload images or videos."
})
# 从文件列表依次取出并保存,文件名与上传时一致
for image_file in image_files:
image_file.save(image_file.filename)
# 同上
for video_file in video_files:
video_file.save(video_file.filename)
return jsonify({
"code": 0,
"message": "upload images and videos success."
})
if __name__ == '__main__':
# 启动flask app
app.run('0.0.0.0', debug=True, port=5000)
喜欢的可以点击关注,不迷路,后续给封装起来供大家使用!