我正在编写一个脚本,使用松弛api和Python中的请求库从Slack下载文件。每当我下载一个文件,它们都会显示出相同的大小(80 are ),并且它们都被破坏了。
这是我的代码:
def download_file(url, out):
try:
os.stat(out)
except:
os.mkdir(out)
local_filename = out + '\\' + url.split('/')[-1]
print('outputting to file: %s' % local_filename)
response = requests.get(url, stream=True)
with open(local_filename, 'wb') as f:
response.raw.decode_content = True
shutil.copyfileobj(response.raw,f)
return local_filename我尝试了各种不同的方法张贴自始至终,所以下载文件,但没有成功。我还检查了从Slack获得的URL,它们是正确的,因为我可以将它们粘贴到浏览器中并下载文件。
任何帮助都将不胜感激!
发布于 2018-06-14 02:45:46
我解决了我的问题。由于我使用的是从Slack文件对象下载的图像的私有url,所以除了带有令牌的基本请求之外,我还需要包含一个标头。要做到这一点,可以使用requests:
response = request.get(url,stream = True, headers={'Authorization':'Bearer ' + my_token})
https://stackoverflow.com/questions/50809167
复制相似问题