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



我正在尝试从Internet下载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)

与上面的卷曲命令的确切等效是什么?

从文档中,这就是您应该读取 ->写蒸汽的方式:

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

最新更新