爬虫基础-塔卡库urllib和urllib30301_第1页
爬虫基础-塔卡库urllib和urllib30301_第2页
爬虫基础-塔卡库urllib和urllib30301_第3页
爬虫基础-塔卡库urllib和urllib30301_第4页
爬虫基础-塔卡库urllib和urllib30301_第5页
已阅读5页,还剩18页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Python爬虫基础课教室讲师:塔卡 时间:2018年4月教学监督热线 教学监督热线 01了解fiddler抓包工具的基本使用知识点回顾本节知识点01. 掌握urllib模块02. 了解urllib3模块教学监督热线 urllib知识点一教学监督热线 urllib库中的4大模块01020304urllib.requests 请求模块urllib.parse 解析模块urllib.error 异常处理模块urllib.robotparse robots.txt解析模块urllib.request内容urlopen基本请求添加header头信息使用cookie设置代理发出请求: urllib.re

2、quest.urlopenfrom urllib import requesturl = rs = request.urlopen(url)print(rs)# print(rs.read()print(rs.read().decode(utf-8) # 读取网页源代码print(rs.getcode() # 状态码print(rs.headers) # 和info一样的,返回的头信息print()print(rs.geturl() # 返回请求的url例:使用Request,添加heardersfrom urllib import requesturl = headers = User-Ag

3、ent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36,req = request.Request(url,headers=headers) #添加headers头信息rs1 = request.urlopen(req)print(rs1)例:from urllib import parsex = parse.quote(潭州) # url 编码print(x)y = parse.unquote(x) # ur

4、l解码print(y)url3 = .format(x)rs = request.urlopen(url3)urllib.parse.quote/urllib.parse.unquote例:d1 = a:1,b:2x = parse.urlencode(d1) # 转成字符串print(x)url3 = y = /?wd=pythonz = parse.urljoin(url3,y)print(z)urllib.parse.urlencode/urllib.parse.urljoin例:urllib.parse需掌握的方法:urllib.parse.quote() # url 中文编码urll

5、ib.parse.unquote() # url中文解码urllib.parse.urlencode() # 转成&拼接的字符串urllib.parse.urljoin() # url拼接get/post方法请求urllib库,默认是get方式提交,urlopen的data参数默认为None,当data参数不为空的时候,urlopen提交方式为Post(request的get_method),如果要使用其他方式必须Request(method=options)或req.get_method = lambda : options。urllib.request.Request(url, data=

6、None, headers=, method=None)get方法请求data = urllib.parse.urlencode(data) #对data进行转码,转化成str类型new_url = url+?+data # URL拼接result = urllib.request.urlopen(new_url) response = result.read() print(response.decode(utf8) url = 请求的地址/URL” data = 提交的数据post方法请求data = urllib.parse.urlencode(data) # 对data进行转码,转化成

7、str类型data = data.encode(utf-8) #进行编码,post请求只支持byte类型new_url = urllib.request.Request(url,data) #对url和参数进行包装response = urllib.request.urlopen(new_url) response = result.read() print(response.decode(utf8) url = 请求的地址/URL data = 提交的数据cookie的使用from http import cookiejarurl = # 创建cookiejar对象cookie = cook

8、iejar.CookieJar()# 使用HTTPCookieProcessor创建cookie处理器,并以它为参数构建opener对象opener = request.build_opener(request.HTTPCookieProcessor(cookie)# 把opner变成urlopenrequest.install_opener(opener)rs = request.urlopen(url)print(cookie)例:设置代理:url5 = # 测试ip地址的网站proxy = http:ip:port,https:ip:port# 创建代理处理器proxies = requ

9、est.ProxyHandler(proxy)# 创建opener对象opener = request.build_opener(proxies,request.HTTPHandler )request.install_opener(opener)data = request.urlopen(url5)print(data.read().decode()例:# error异常处理 URLError是HTTPError的父类 只有HTTPError有code属性url1 = url2 = try: request.urlopen(url1)except error.URLError as e:

10、if hasattr(e,code): print(HTTPError) elif hasattr(e,reason): print(URLError)error异常处理:例:urllib3知识点二教学监督热线 PoolManager:# PoolManager实例来生成请求,由该实例对象处理与线程池的连接以及线程安全的所有细节,不需要任何人为操作:http = urllib3.PoolManager()# 通过request()方法创建一个请求resp = http.request(get,url)html = resp.dataprint(html.decode()HTTPConnecti

11、onPool:url = http = urllib3.HTTPConnectionPool( )resp = http.request(get,url)print(resp.data.decode(gbk)设置代理:import urllib3proxy = urllib3.ProxyManager( , headers=connection: keep-alive)r = proxy.request(GET, )print(r.status)print(r.data)操作cookie:没有找到相关的操作,只能通过headers设置的方式实现http访问库的需求总结- 正确发出http、https请求-

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论