使用python请求库进行PDF编码已损坏

使用python请求库进行PDF编码已损坏,python,json,pdf,python-requests,Python,Json,Pdf,Python Requests,我使用Python请求并迭代一个JSON响应,然后使用请求库发送另一个请求,根据JSON中的参数下载一个文件,如下所示: url = attachmentIndex['url'] # location of the file response = requests.get(url, stream=True, auth=('xxxxx', 'xxxxx')) with open(attachmentIndex['name'], 'wb') as out_file: shutil.copyf

我使用Python请求并迭代一个JSON响应,然后使用请求库发送另一个请求,根据JSON中的参数下载一个文件,如下所示:

url = attachmentIndex['url'] # location of the file
response = requests.get(url, stream=True, auth=('xxxxx', 'xxxxx'))
with open(attachmentIndex['name'], 'wb') as out_file:
    shutil.copyfileobj(response.raw, out_file)
del response
我面临的问题是PDF文件的编码很奇怪,如果我试图打开一个,它表明文件已损坏。以下是我在文本编辑器中打开保存的PDF文件时最重要内容的屏幕截图:


我可以用python做些什么来正确设置PDF格式吗?

请尝试设置
response.raw.decode\u content=True
copyfileobj@nathancahill很好,非常感谢!