当前位置:   article > 正文

[python]python下载文件几种方法_python download file

python download file

如果您的文件比较小,可以采用下面的方法

使用urllib

  1. import urllib.request
  2. url = "http://example.com/video.mp4"
  3. path = "downloaded_file.video"
  4. urllib.request.urlretrieve(url, path)

使用requests

  1. import requests
  2. url = "http://example.com/video.mp4"
  3. path = "downloaded_file.video"
  4. r = requests.get(url)
  5. with open(path, "wb") as code:
  6. code.write(r.content)

 使用wget

  1. import wget
  2. url = "http://example.com/video.mp4"
  3. path = "downloaded_file.video"
  4. wget.download(url, out=path)

大文件下载可以采用下面方法:

方法一:

  1. import requests
  2. def download_file(url, save_path):
  3. response = requests.get(url, stream=True, verify=False)
  4. response.raise_for_status()
  5. with open(save_path, 'wb') as file:
  6. for chunk in response.iter_content(chunk_size=8192):
  7. file.write(chunk)

 方法二:

  1. import urllib2
  2. def download_file_urllib(url,save_path)
  3. r = urllib2.Request(url)
  4. u = urllib2.urlopen(r)
  5. with open(save_path, 'w') as f:
  6. while True:
  7. tmp = u.read(1024)
  8. if not tmp:
  9. break
  10. f.write(tmp)

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/814492
推荐阅读
相关标签
  

闽ICP备14008679号