Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can python-requests fetch url directly to file handle on disk like curl?

curl has an option to directly save file and header data on disk:

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

Is there same ability in python-requests ?

like image 505
rsk82 Avatar asked Sep 03 '25 05:09

rsk82


1 Answers

As far as I know, requests does not provide a function that save content to a file.

import requests

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

See 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.

like image 124
falsetru Avatar answered Sep 04 '25 18:09

falsetru