Skip to content

Instantly share code, notes, and snippets.

@ayuLiao
Last active November 1, 2018 06:03
Show Gist options
  • Select an option

  • Save ayuLiao/d66f649beb73086d14516443e9c17b29 to your computer and use it in GitHub Desktop.

Select an option

Save ayuLiao/d66f649beb73086d14516443e9c17b29 to your computer and use it in GitHub Desktop.
python文件下载
import requests
# 下载普通文件
def download_file():
image_url = "https://www.python.org/static/community_logos/python-logo-master-v3-TM.png"
r = requests.get(image_url) # create HTTP response object
with open("python_logo.png",'wb') as f:
f.write(r.content)
# 下载大文件,减小内存占用,切片形式下载
def download_bigfile():
file_url = "http://codex.cs.yale.edu/avi/db-book/db4/slide-dir/ch1-2.pdf"
r = requests.get(file_url, stream=True)
with open("python.pdf", "wb") as pdf:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
pdf.write(chunk)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment