请教一下Python大佬,requests.post上传文件到网站错误怎么破?

搬瓦工机场JMS

比如我要通过Python脚本上传文件到https://anonfile.com/这个网站上,在网上查了很多资料写了脚本还是失败

  1. import requests
  2. headers={‘User-Agent’:’Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36′}
  3. url = ‘https://anonfile.com/api/upload’
  4. files = {‘files[]’: open(‘C:\\Users\\Admin\\Desktop\hello.txt’, ‘rb’)}
  5. response = requests.post(url, files=files,headers = headers)
  6. print(response.text)

复制代码

显示错误

  1. {"status":false,"error":{"message":"No file chosen.","type":"ERROR_FILE_NOT_PROVIDED","code":10}}

复制代码

求大佬指点啊
江西网友:
上传文件 没有进度条 等得心慌 更新个带进度条的
这个进度条是在命令行下的, idl的控制台不显示

  1. from clint.textui.progress import Bar as ProgressBar
  2. from requests_toolbelt import MultipartEncoder, MultipartEncoderMonitor
  3. import requests, re, os
  4. def create_callback(encoder):
  5.     bar = ProgressBar(expected_size=encoder.len, filled_char=’=’)
  6.     def callback(monitor):
  7.         bar.show(monitor.bytes_read)
  8.     return callback
  9. def get_token(indexHtml):
  10.     r = re.search(r’\’X-CSRF-Token\’: \'(.+?)\”, indexHtml)
  11.     return r.group(1)
  12. if __name__ == ‘__main__’:
  13.     url = ‘https://anonfile.com’
  14.     api = ‘https://anonfile.com/api/upload’
  15.     headers = {
  16.         ‘user-agent’: ‘Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36’,
  17.     }
  18.     session = requests.session()
  19.     r = session.get(url, headers=headers)
  20.     headers[‘x-csrf-token’] = get_token(r.text)
  21.     headers[‘referer’] = ‘https://anonfile.com/’
  22.     # 文件地址就写这里了
  23.     encoder = MultipartEncoder({‘file’: (‘kblm.jpg’, open(‘kblm.jpg’, ‘rb’), ‘application/octet-stream’)})
  24.     callback = create_callback(encoder)
  25.     monitor = MultipartEncoderMonitor(encoder, callback)
  26.     headers[‘Content-Type’] = monitor.content_type
  27.     r = session.post(api, data=monitor, headers=headers)
  28.     print(‘\nUpload finished! (Returned status {0} {1})’.format(
  29.         r.status_code, r.reason
  30.     ))
  31.     print(r.text)

复制代码

  1. [================================] 116933/116933 – 00:00:00
  2. Upload finished! (Returned status 200 OK)
  3. {"status":true,"data":{"file":{"url":{"full":"https://anonfile.com/B329l2tfn3/kblm_jpg","short":"https://anonfile.com/B329l2tfn3"},"metadata":{"id":"B329l2tfn3","name":"kblm.jpg","size":{"bytes":116749,"readable":"116.75 KB"}}}}}

复制代码

山西网友:
跟随大佬学习下py传文件
重庆网友:看这个返回意思是没有对应文件……

“如果你发送一个非常大的文件作为 multipart/form-data 请求,你可能希望将请求做成数据流。默认下 requests 不支持, 但有个第三方包 requests-toolbelt 是支持的。你可以阅读 toolbelt 文档 来了解使用方法。”

看返回好像是没有找到对应文件……

甘肃网友:你先把文件拷贝到脚本同目录上传看看
湖北网友:mk
海南网友:哇,可以了,谢谢python大佬

我来好好学习一下您的代码
西藏网友:谢谢,看来 requests-toolbelt是正确的选择

以上信息转载自Hostloc。

未经允许不得转载:美国VPS_搬瓦工CN2 GIA VPS » 请教一下Python大佬,requests.post上传文件到网站错误怎么破?

赞 (0) 打赏

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏