python2 与python3的urllib不同在与python3要加上.request
比如:
#python3
import urllib.request
res=urllib.request.urlopen(html)
data = urllib.request.urlretrieve("http://...")
#python2
import urllib
print "downloading with urllib"
url = 'http://www.pythontab.com/test/demo.zip'
print "downloading with urllib"
urllib.urlretrieve(url, "demo.zip")
下载tomcat的一个例子‘
# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @Author: name
import os,urllib.request
# os 用来将路径和文件名拼接在一起,组成一个绝对路径
url = 'http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.43/bin/apache-tomcat-8.5.43.zip'
mk = '/Users/Desktop/'
filename = 'tomcat.zip'
dest_dir = os.path.join(mk, filename)
def testformurl():
try:
urllib.request.urlretrieve(url, dest_dir)
except Exception as e:
print(e)
print("is wrong ")
testformurl()