urllib简单用法和其中的handler、代理
https://www.cnblogs.com/miqi1992/p/7872785.html
https://blog.csdn.net/topleeyap/article/details/78845829
#https://www.cnblogs.com/miqi1992/p/7872785.html 内含(西刺免费代理、快代理免费代理、Proxy360代理、全网代理ip)
1.基本网页请求 urlopen(url,data,timeout) --urlopen只有这几个参数(没有headers参数) --data参数(要传入bytes类型的,要将字典先转化为字符串): --import urllib.parse import urllib.request data=bytes(urllib.parse.urlencode({"data":"xx"}),encoding="utf8") #parse用来将字典转化为字符串 respone=urllib.request.urlopen(url,data=data) 2.请求中要加入headers等信息,要用到urllib.request.Request urllib.request.Request(url,data,headers={},method) --data参数(要传入bytes类型的,要将字典先转化为字符串) --from urllib import request,parse url="xx" headers={ xx } dict={"name":"x"} data=bytes(parse.urlencode(dict),encoding="urf8") req=request.Request(url,data,headers=headers) #req=request.Request(url=url,data=data,method="POST") #req.add_header("user-agent","Mozilla/4.0...") respone=request.urlopen(req) 3.cookie处理、代理设置要用到opener
# 构建handle处理器一般分为以下3步:
import urllib2 http_handler = urllib2.HTTPHandler(); # 1.构建特定功能的处理器对象 opener = urllib2.build_opener(http_handler) # 2.通过urllib2.build_opener(http_handler)方法来使用这些对象,创建成自定义opener对象 request = urllib2.Request("http://www.baidu.com") response = opener.open(request) # 3.使用创建成的opener对象,调用open()方法来发送请求
print(response.read())

浙公网安备 33010602011771号