收到200条回应,但回应为空

时间:2019-01-14 19:13:55

标签: python-3.x python-requests

我正在使用python中的Requests发出一些原始的Http请求,以从网站中获取一些数据。

headerData = {
    # "Accept": "*/*",
    # "Accept-Encoding": "gzip, deflate, br",
    # "Accept-Language": "en-US,en;q=0.9",
    "Connection": "keep-alive",
    "Content-Type": "application/json",
    "Cookie": cookie,
    "Host": host,
    "jwt": newJWT,
    "Referer": referer,
    "User-Agent": agent,
    "X-Requested-With": "XMLHttpRequest"
}
# I don't know why I need to request this url twice, I just followed the same pattern as web browser's actions.
response = requests.get(dataUrl, headers=headerData, stream=True)
print(response.content)
response3 = requests.get(dataUrl, headers=headerData, stream=True)
print(response.content)

if response3.status_code != 200:
    raise ValueError('Failed to download')
params = cgi.parse_header(response3.headers.get('Content-Disposition', ''))[-1]
if 'filename' not in params:
    raise ValueError('Could not find a filename')
filename = os.path.basename(params["filename"])
abs_path = os.path.join('/Users/qijeremy/Desktop/data', filename)
with open(abs_path, 'wb') as target:
    response3.raw.decode_content = True
    shutil.copyfileobj(response3.raw, target)

print(filename)

控制台上的输出是

/Users/qijeremy/venv/bin/python /Users/qijeremy/PycharmProjects/scripts/AIMScript.py


23c322d3-e83e-2d38-04e0-a4af3ef8c254.dat

Process finished with exit code 0

内容为空,但是如果您查看标题,状态码为200。并且附件的文件名为Content-Dsiposition,带有Content-Length = 0

结果显示在Web浏览器中: Image1

它还具有以下内容: Image2

0 个答案:

没有答案