import urllib.request
with urllib.request.urlopen("http://www.baidu.com") as file:
data = file.read() # 读取全部
line = file.readline() # 读取一行
lines = file.readlines() #将全部文件组成一个按行组成的列表并返回
with open("./1.html","wb") as f:
f.write(data)
filename = urllib.request.urlretrieve("http://www.baidu.com","./2.html")
file.info()
file.getcode()
200
file.geturl()
'http://www.baidu.com'
file.getheaders()
[('Date', 'Mon, 09 Apr 2018 17:11:24 GMT'),
('Content-Type', 'text/html; charset=utf-8'),
('Transfer-Encoding', 'chunked'),
('Connection', 'Close'),
('Vary', 'Accept-Encoding'),
('Set-Cookie',
'BAIDUID=4B4DEF37A228ED2722DF818D3F4A6C29:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com'),
('Set-Cookie',
'BIDUPSID=4B4DEF37A228ED2722DF818D3F4A6C29; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com'),
('Set-Cookie',
'PSTM=1523293884; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com'),
('Set-Cookie', 'BDSVRTM=0; path=/'),
('Set-Cookie', 'BD_HOME=0; path=/'),
('Set-Cookie', 'H_PS_PSSID=1430_21090_22160; path=/; domain=.baidu.com'),
('P3P', 'CP=" OTI DSP COR IVA OUR IND COM "'),
('Cache-Control', 'private'),
('Cxy_all', 'baidu+230416a5fbb4a587682dea3e4efe4e59'),
('Expires', 'Mon, 09 Apr 2018 17:11:05 GMT'),
('X-Powered-By', 'HPHP'),
('Server', 'BWS/1.1'),
('X-UA-Compatible', 'IE=Edge,chrome=1'),
('BDPAGETYPE', '1'),
('BDQID', '0xab6114e500016321'),
('BDUSERID', '0')]
使用quote进行编码,再使用unquote进行解码
s = urllib.request.quote("http://www.baidu.com")
s
'http%3A//www.baidu.com'
urllib.request.unquote(s)
'http://www.baidu.com'