Python/Spider: Difference between revisions
< Python
Jump to navigation
Jump to search
(Created page with "<source 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:...") |
No edit summary |
||
| (One intermediate revision by the same user not shown) | |||
| Line 1: | Line 1: | ||
== Soup == | |||
<source lang="python"> | |||
</source> | |||
== Download == | |||
<source lang="python"> | <source lang="python"> | ||
def download_file(url): | def download_file(url): | ||
Latest revision as of 03:46, 6 August 2019
Soup
Download
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