• python3的urllib包有了一些更新,在使用的时候要注意一下
1
2
3
4
5
6
import os
import urllib
url = ""
file = "{}.pdf".format('download_file')
save_path = os.path.join('/Users/Desktop/', file)
urllib.request.urlretrieve(url, save_path)
  • 这样使用会报错
1
AttributeError: module 'urllib' has no attribute 'request'
  • 因为在python3中urllib.request成为了一个独立的包, 所以在导入的时候要改为
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import os
import urllib.request as ur
url = ""
file = "{}.pdf".format('download_file')
save_path = os.path.join('/Users/Desktop/', file)

# 加headers
headers = [
('Content-Type', 'application/x-www-form-urlencoded'),
("user-agent", "212313132131514654")
]
opener = ur.build_opener()
opener.addheaders = headers
ur.install_opener(opener)

# 下载保存
ur.urlretrieve(url, save_path)

其他下载文件方法

1
2
3
4
headers = {'Content-Type': "application/json"}
r = requests.request("GET", url, headers=headers)
with open("download_file.xls", "wb") as code:
code.write(r.content)