1、urllib简介以及Urllib.Parse的使用
- URLLIB是python自带标准库中的用于网络请求的库,无需安装,直接引入即可
- 通常用来爬虫开发,APL(应用程序接口),数据获取和测试
- urllib库的四大模板
- urllib.request: 用于打开和读取URL
- urllib.error:包含提出的例外(异常)urllib.request
- urllib.parse:用于解析URL
- urllib.robotparser:用于解析robots.txt文件
#urllib.parse 用于解析url
import urllib.parse
kw={'wd':'茜茜'}
#编码
#第一次复习
#编码(请求参数是中文需要编码)
result=urllib.parse.urlencode(kw)
print(result) #输出:wd=%E8%8C%9C%E8%8C%9C 因为地址栏不能有中文,所以使用urllib.parse来进行编码
#解码
res=urllib.parse.unquote(result)
print(res) #输出: wd=茜茜
2,Urllib.Request的get和post请求
- urllib.request库 模拟浏览器发起一个HTTP请求,并获取请求响应结果
- urllib.request.urlopen的语法格式 urlopen(url, data=None, [timeout,]*, cafile=None, capath=None, cadefault=False, context=None)
- 参数说明 url:url参数是str类型的地址,也就是要访问的URL,例如:https://www.baidu.com data:默认值为None,urllib判断参数data是否为None从而区分请求方式。或参数data为None,则代表请求方式为Get,反之请求方式为Post,发送Post请求。参数data以字典形式存储数据,并将参数data由字典类型转换成字节类型才能完成Post请求
- urlopen函数返回的结果是一个http.client.HTTPResponse对象
import urllib.request
url='http://www.biquger.net/'
#发送请求
resp=urllib.request.urlopen(url)
html=resp.read().decode('gbk') #decode 是将bytes类型转换为 str 类型
print(html)
登录网页的post请求:
#登录的post请求
import urllib.request
import urllib.parse
url='http://www.biquger.net/login.php?jumpurl=%2Findex.php'
data={'username':'15070196362','password':'bqg2660367','action':'login','checkcode':'1418'}
#一轮
#发送请求
resp=urllib.request.urlopen(url,data=bytes(urllib.parse.urlencode(data),encoding='utf-8'))
html=resp.read().decode('gbk')
print(html)
发送请求-Request请求
- Request 如果在请求中需要加入headers (请求头)、指定请求方式等信息,那么就可以利用更加大的Request来构建一个请求。
- 语法 urllib.request.Request(url, data=None, headers={}, method=None)
反爬网站应对
import urllib.parse
import urllib.request
#反爬的网站应对
url='https://www.douban.com/'
#加入请求头伪装成浏览器
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36'}
#一轮
#构建请求对象
req=urllib.request.Request(url,headers=headers)
#从响应结果中打开请求
resp=urllib.request.urlopen(req)
#从响应结果中读取数据
html=resp.read().decode('utf-8')
print(html)
IP代理
-
opener的使用
elif context: https_handler = HTTPSHandler(context=context) opener = build_opener(https_handler) elif _opener is None: _opener = opener = build_opener() else: opener = _opener return opener.open(url, data, timeout)
- 构建自己的opener发送请求
import urllib.request
url='http://www.baidu.com'
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36'}
#构建请求对象
req=urllib.request.Request(url,headers=headers)
#一轮
#获取opener对象
opener=urllib.request.build_opener()
resp=opener.open(req)
print(resp.read().decode())
IP代理
- 为什么需要使用IP代理 假如一个网站它会检测某一段时间某个IP的访问次数,如果访问次数过多,它会禁止你的访问。所以你可以设置一些代理服务器来帮助你做工作,每隔一段时间换一个代理。
- IP代理的分类
- 透明代理:目标网站知道你使用了代理并且知道你的源IP地址,这种代理显然不符合我们这里使用代理的初衷
- 匿名代理:匿名程序比较低,也就是网站知道你使用了代理,但是并不知道你的源IP地址
- 高匿代理:这是最保险的方式,目录网站既不知道你使用了代理更不知道你的源IP
#一轮
#导入创建对象
import urllib.request
from urllib.request import build_opener
#导入代理
from urllib.request import ProxyHandler
#免费高匿网站:https://free.kuaidaili.com/free
proxy=ProxyHandler({'http':'58.20.184.187:9091'})
opener=build_opener(proxy)