Plone技术资料

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 7706|回复: 0

下载文件的python脚本赏析

[复制链接]
发表于 2011-10-21 16:18:39 | 显示全部楼层 |阅读模式
下载文件的python脚本

  1. from os.path import basename
  2. from urlparse import urlsplit

  3. def url2name(url):
  4.     return basename(urlsplit(url)[2])
  5. def download(url, localFileName = None):
  6. localName = url2name(url)
  7. req = urllib2.Request(url)
  8. r = urllib2.urlopen(req)
  9. if r.info().has_key('Content-Disposition'):
  10. # If the response has Content-Disposition, we take file name from it
  11. localName = r.info()['Content-Disposition'].split('filename=')[1]
  12. if localName[0] == '"' or localName[0] == "'":
  13. localName = localName[1:-1]
  14. elif r.url != url:
  15. # if we were redirected, the real file name we take from the final URL
  16. localName = url2name(r.url)
  17. if localFileName:
  18. # we can force to save the file as specified name
  19. localName = localFileName
  20. f = open(localName, 'wb')
  21. f.write(r.read())
  22. f.close()


  23. download(r'你要下载的python文件的url地址')
复制代码

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|Plone技术资料 ( 湘ICP备14006519号-1 )

GMT+8, 2019-11-21 06:54 , Processed in 0.045908 second(s), 18 queries , Gzip On.

Powered by Plone! X3.4

© 2001-2019 Plone.org.

快速回复 返回顶部 返回列表