奇怪的是,请求没有什么简单的方法。您必须遍历响应并将这些块写入文件:
response = requests.get('http://www.example.com/image.jpg', stream=True)# Throw an error for bad status presresponse.raise_for_status()with open('output.jpg', 'wb') as handle: for block in response.iter_content(1024): handle.write(block)
我通常只是使用
urllib.urlretrieve()。它可以工作,但是如果您需要使用会话或某种身份验证,那么上面的代码也可以工作。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)