Python的'请求'相当于'curl -o'

时间:2017-09-19 23:39:56

标签: python curl python-requests

我正在尝试从互联网上下载pdf文件。我可以使用以下命令下载它:

curl -o test.pdf <url>

我尝试使用“请求”来获取它,但它失败了。我做了以下事情:

# Where <url> is the actual URL
r = requests.get(<url>, stream=True)
with open("test.pdf", 'wb') as f:
    f.write(r.content)

上面我的curl命令的确切等价是什么?

1 个答案:

答案 0 :(得分:0)

documentation开始,您应该阅读以下内容:&gt;写一个简介:

r = requests.get(<url>, stream=True)
with open(filename, 'wb') as fd:
    for chunk in r.iter_content(chunk_size=128):
        fd.write(chunk)
相关问题