from urllib import request
#1.urlopen 访问网页
#--------------------------------------------------------------
resp = request.urlopen('http://www.baidu.com')
# print(resp.read(100)) #读取字节
# print(resp.read()) #所有代码
# print(resp.read().decode('utf-8')) #编译所有后代码
# print(resp.readline()) #读取一行
# print(resp.readlines()) #读取多行
# print(resp.getcode()) #读取响应码
#2.urlretrieve 下载文件网页
#--------------------------------------------------------------
request.urlretrieve('http://www.baidu.com','baidu.html')
#括号里加网址,后面加存放地址和文件属性(可以网址可以图片等等)
#3.urlencode 编码
#--------------------------------------------------------------
from urllib import parse
params = {"name":'huang','age':19 ,'word':'hello'}
result = parse.urlencode(params)
# print(result)
#--------------------------------------------------------------
from urllib import parse
url = 'http://www.baidu.com/s'
params = {'wd':'马云'}
qs = parse.urlencode(params)
url = url + "?" +qs
res = request.urlopen(url)
print(url)
# print(res.read())
#4.parse 解码
#--------------------------------------------------------------
from urllib import parse
params = {"name":'huang','age':19 ,'word':'hello'}
qs = parse.urlencode(params)
resu = parse.parse_qs(qs)
print(resu)
#5.urlparse和urlsplit,差不多是一样的,除了parse中多了一个 params属性
#--------------------------------------------------------------
from urllib import parse
url = 'https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=马云'
html_url = parse.urlparse(url)
print(html_url)
print('path',html_url.path)
ht_url = parse.urlsplit(url)
print(ht_url)