西麦农场

时间尺度守恒


  • 首页

  • 分类

  • 归档

  • 坑List

Python中从URL中获取文件名使用requests下载文件

2019-12-05 | DevFAQ | python requests

代码如下

  1. def download_file(dir_path, url):
  2. path = urlparse.urlsplit(url).path
  3. filename = posixpath.basename(path)
  4. r = requests.get(url, stream=True)
  5. print r.status_code
  6. if r.status_code == 200:
  7. with open(os.path.join(dir_path, filename), "wb") as f:
  8. for chunk in r.iter_content(chunk_size=512):
  9. if chunk:
  10. f.write(chunk)
  • 目录
  • 站点概览
  • 代码如下
    戊在西安填坑中

    戊在西安填坑中

    29 日志
    7 分类
    50 标签
    0%
    © 2017- 戊在西安填坑中 | 陕ICP备13008306号-3 | 主题 NexT.Pisces