handler 用法详解
#创建handler,针对于http请求
handler = urllib.request.HTTPHandler()
#opener就是一个对象,一会发送请求的时候,直接使用opener里面的方法open,不要使用urlopen了
opener = urllib.request.build_opener(handler)
使用在cookie上
import urllib.request
import urllib.parse
import http.cookiejar
# 真实的模拟浏览器,当发送完post请求的时候,将cookie保存到代码中
# 创建一个cookiejar对象
cj = http.cookiejar.CookieJar()
# 通过cookiejar创建一个handler
handler = urllib.request.HTTPCookieProcessor(cj)
# 根据handler创建一个opener
opener = urllib.request.build_opener(handler)
post_url = 'http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=201843935495'
formdata = {
'email': '17701256561',
'icode': '',
'origURL': 'http://www.renren.com/home',
'domain': 'renren.com',
'key_id': '1',
'captcha_type': 'web_login',
'password': '3f410d8973fe8372d1500bfc877b475b1e3f0cec62ab8cf28f7024fdeb6e2a99',
'rkey': '5359624622816e72df1281517296edda',
'f': 'https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DYxPWlNcZg9d5hfNaZgVM3_qb_yK0LL92LzZ4FeynfMq%26wd%3D%26eqid%3Dfc4556f900040a75000000035b0df91b',
}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36',
}
request = urllib.request.Request(url=post_url, headers=headers)
formdata = urllib.parse.urlencode(formdata).encode()
response = opener.open(request, data=formdata)
print(response.read().decode())
print('*' * 50)
get_url = 'http://www.renren.com/960481378/profile'
request = urllib.request.Request(url=get_url, headers=headers)
response = opener.open(request)
print(response.read().decode())
#使用在代理上
import urllib.request
import urllib.parse
# 113.79.75.104:9797
# 创建handler,代理服务器
handler = urllib.request.ProxyHandler({'http': '114.215.95.188:3128'})
# 常见opener
opener = urllib.request.build_opener(handler)
url = 'http://www.baidu.com/s?ie=UTF-8&wd=ip'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36',
}
request = urllib.request.Request(url, headers=headers)
response = opener.open(request)
with open('ip.html', 'wb') as fp:
fp.write(response.read())