- 论坛徽章:
- 0
|
这是我下载文件的代码,你可以参考参考
## Download files
import sys, os, urllib2
PATH = r'D:\SF\test'
def buildproxy():
'''
build a new opener that uses a proxy requiring authorization
'''
username = 'test'
password = 'test1'
proxy_info = {
'user' : username,
'pass' : password,
'host' : 'proxy,
'port' : 8080
}
# build a new opener that uses a proxy requiring authorization
proxy_support = urllib2.ProxyHandler({'http' : \
'http://%(user)s:%(pass)s@%(host)s:%(port)d' % proxy_info})
opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler)
#print 'http://%(user)s:%(pass)s@%(host)s:%(port)d' % proxy_info
# install it
urllib2.install_opener(opener)
def main():
if len(sys.argv) != 2: exit('Please specify the URL to download')
buildproxy()
target = sys.argv[1]
filename = os.path.join(PATH, urllib2.unquote(target.split('/')[-1]))
## print filename
try:
content = urllib2.urlopen(target).read()
open(filename,'wb').write(content)
print 'Save file to %s' % filename
except:
print 'Failed to get file'
if __name__ == '__main__':
main() |
|
|