Python: Difference between revisions

618 bytes added ,  26 February 2020
Line 208: Line 208:
pastebin_url = r.text  
pastebin_url = r.text  
print("The pastebin URL is:%s"%pastebin_url)  
print("The pastebin URL is:%s"%pastebin_url)  
</syntaxhighlight>
====Download a file====
[https://stackoverflow.com/questions/16694907/download-large-file-in-python-with-requests SO Answer]
<syntaxhighlight lang="python">
def download_file(url):
    local_filename = url.split('/')[-1]
    # NOTE the stream=True parameter below
    with requests.get(url, stream=True) as r:
        r.raise_for_status()
        with open(local_filename, 'wb') as f:
            for chunk in r.iter_content(chunk_size=8192):
                if chunk: # filter out keep-alive new chunks
                    f.write(chunk)
                    # f.flush()
    return local_filename
</syntaxhighlight>
</syntaxhighlight>