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 ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With