请求
# 请求url
#encoding: utf-8
from urllib import request
# 编码相关
from urllib import parse
# 一. 网络请求
# resp = request.urlopen('http://www.baidu.com')
# print(resp.read())
# 二. 下载
# request.urlretrieve('http://www.baidu.com/','baidu.html')
# 三. 带参数的请求
# url = 'http://www.baidu.com/s'
# data = {'wd':'刘德华'}
# # 编码
# qs = parse.urlencode(data)
# url = url + '?' + qs
# resp = request.urlopen(url)
# print(resp.read())
# 解码
# qs = "name=%E7%88%AC%E8%99%AB%E5%9F%BA%E7%A1%80&greet=hello+world&age=100"
# print(parse.parse_qs(qs))
# 四. URL分割
url = 'http://www.baidu.com/s?username=zhiliao&userid=66'
# result = parse.urlsplit(url)
result = parse.urlparse(url)
print('scheme:',result.scheme)
print('netloc:',result.netloc)
print('path:',result.path)
# urlparse才有params
print('params:',result.params)
print('query:',result.query)
print('fragment:',result.fragment)
# 设置代理
#encoding: utf-8
# 设置代理
from urllib import request
url = 'https://httpbin.org/ip'
url2 = 'http://ip-api.com/json'
# 这个是没有使用代理的
# resp = request.urlopen(url)
# print(resp.read().decode("utf-8"))
# 这个是使用了代理的
handler = request.ProxyHandler({"HTTP":"171.35.222.24:9999"})
opener = request.build_opener(handler)
# req = request.Request(url)
# resp = opener.open(req)
resp = opener.open(url)
print(resp.read())
上次更新: 2022/08/25, 10:58:25