gpt4 book ai didi

python - python-requests 可以像 curl 一样直接获取 url 到磁盘上的文件句柄吗?

转载 作者:行者123 更新时间:2023-11-28 21:23:06 25 4
gpt4 key购买 nike

curl 有一个选项可以直接将文件和头数据保存在磁盘上:

curl_setopt($curl_obj, CURLOPT_WRITEHEADER, $header_handle);
curl_setopt($curl_obj, CURLOPT_FILE, $file_handle);

python-requests 有同样的能力吗?

最佳答案

据我所知,requests 没有提供将内容保存到文件的功能。

import requests

with open('local-file', 'wb') as f:
r = requests.get('url', stream=True)
f.writelines(r.iter_content(1024))

参见 request.Response.iter_content documentation .

iter_content(chunk_size=1, decode_unicode=False)

Iterates over the response data. When stream=True is set on the request, this avoids reading the content at once into memory for large responses. The chunk size is the number of bytes it should read into memory. This is not necessarily the length of each item returned as decoding can take place.

关于python - python-requests 可以像 curl 一样直接获取 url 到磁盘上的文件句柄吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17953210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com