Requests库的入门
来源:互联网 发布:dede cms 编辑:程序博客网 时间:2024/06/05 15:24
1.cmd(管理员权限)--->pip install requests
2.Requests库的7个方法
requests.request() 构造一个请求,支撑以下各方法的基础方法
requests.get() 获取HTML网页的主要方法,对应于HTTP的GET
requests.head() 获取HTML网页头信息的方法,对应于HTTP的HEAD
requests.post() 向HTML网页提交POST请求的方法,对应于HTTP的HEAD
requests.put() 向HTML网页提交PUT请求的方法,对应于HTTP的PUT
requests.patch() 向HTML网页提交局部修改请求,对应于HTTP的PATCH
requests.delete() 向HTML页面提交删除请求,对应于HTTP的DELETE
requests.get(url,params=None,**kwargs)
url:拟获取页面的url链接
params:url中的额外参数,字典或字节流格式,,可选
**kwargs:12个控制访问的参数
3.requests对象的属性
r.status_code HTTP请求的返回状态,200表示连接成功,404表示
r.text HTTP响应内容的字符串形式,即url对应的页面内容
r.encoding 从HTTP header中猜测的响应内容编码方式
r.apparent_encoding 从内容中分析的响应内容编码方式
r.content HTTP响应内容的二进制形式
4.requests库的异常
requests.ConnectionError 网络连接错误,如DNS查询失败、拒绝连接
requests.HTTPError HTTP错误异常
requests.URLRequired URL缺失异常
requests.TooManyRedirects 超过最大重定向次数,产生重定向异常
requests.ConnectTimeout 连接远程服务器超时异常
requests.Timeout 请求URL超时,产生超时异常
5.理解requests的异常
r..raise_for_status() 如果不是200,产生异常requests.HTTPError
6.爬取网页通用代码
import requestsdef getHTMLText(url): try: r=requests.get(url,timeout=30) r.raise_for_status() r.encoding=r.apparent_encoding return r.text except: return "产生异常" if __name__=="__main__": url="http://taxq.sdust.edu.cn" print(getHTMLText(url))
7.HTTP协议
HTTP,Hypertext Transfer Protocol 超文本传输协议
HTTP是一个基于“请求与响应”模式的、无状态的应用层协议
HTTP协议采用URL作为定位网络资源的标识
URL格式 http://host[:port][path]
host:合法的Internet主机域名或IP地址
port:端口号,缺省端口为80
path:请求资源的路径
8.requests小结
requests.request()
requests.get()
requests.head()
requests.post
requests.put()
requests.patch()
requests.delete()
- Requests库的入门
- Requests库入门
- Requests库入门
- Requests库入门
- Requests库入门
- Requests 库入门
- requests库入门-1-安装requests库
- requests库入门-4-HTTP请求方法和用requests举例不带参数的请求
- requests库入门-5-带参数的请求类型
- requests库入门-12-response对象的API
- 爬虫入门系列(二):优雅的HTTP库requests
- requests入门
- requests库入门-3-urllib和requests小程序
- Requests库的用法
- requests库的安装
- python的requests库
- requests 库的安装
- Requests库的使用
- js,jquery常用拼接html方法
- HDU 5127 Dogs' Candies(暴力)
- CodeForces #426 div2 B The Festive Evening
- javascript时间戳 转换成时间
- vb.net 为PrintPreviewDialog控件添加新按钮
- Requests库的入门
- Java编程思想之多态
- queue stack priority_queue的基本操作
- 基于位置的服务
- github 代码托管
- MLLib之LogisticRegression
- 优云软件助阵GOPS·2017全球运维大会北京站
- 【ssm框架】 spring+springMVC+mySQL+myBatis+freemarker+Maven小示例
- win10+ubuntu16_04双系统安装