python 实现文件下载

直接输入下载地址就行了,但不能下载txt、bat等文本形式,会出现乱码的情况。

#!/usr/bin/env python

import urllib2
import sys,os


def main(url):
    ext = url[url.rindex("."):]
    filename = url[url.rindex("/")+1:url.rindex(".")]
    folder = os.getcwd()
    try:
        f = urllib2.urlopen(url)
        with open(filename+ext,"wb") as code:
            code.write(f.read())
    except:
        print("parameter error!the url is error?")
        sys.exit(0)
    print("download OK,you can find it in %s\\" %folder)

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("\n\targument error!\n")
	print("\tUseage: file_download.py https://www.xxoo/hehe.zip")
    else:
        main(sys.argv[1])

 求乱码解决方案啊!!

如: https://smarthosts.googlecode.com/svn/trunk/hosts.bat 

posted @ 2013-11-14 11:12  r3call  阅读(1235)  评论(0编辑  收藏  举报