跳转到主内容
趣航编程网 - 趣学编程,启航技术之路!

python中requests库安装与使用详解

目录前言

1、Requests介绍

2、requests库的安装

3、requests库常用的方法

4、response对象的常用属性

5、使用requests发送get请求

5.1  不带参数的get请求5.2 带参数的get请求5.2.1 查询参数params 5.2.2 SSL证书认证参数 verify 5.2.3 设置超时时间 timeout 5.2.4 代理IP参数 proxies 5.3 获取JSON数据5.4 获取二进制数据

6、使用requests发送post请求

7、使用requests的session发送请求

总结前言记得我刚学python-requests库的时候总会有点晕,于是我做了以下关于requests库的知识点整理,方便初学者可以更好的了解requests库。如果有补充或错误,或者不懂的地方,可以评论区留言。

1、Requests介绍

Requests是Python一个很实用的HTTP客户端,完全满足如今网络爬虫的需求urllib库和requests库功能类似,但requests库功能更多更实用

2、requests库的安装

pip命令安装(方法一)

windows操作系统:pip install requests Mac操作系统:pip3 install requests Linux操作系统:sodo pip install requests源码安装(方法二)

下载 requests源码 http://mirrors.aliyun.com/pypi/simple/ requests/下载文件到本地之后,解压到Python安装目录,之后打开解压文运行命令行输入python setup.py install 即可安装测试import requests如果没提示错误,那说明已经安装成功了!

3、requests库常用的方法

序号方法描述1 requests.request(url)构造一个请求,支持以下各种方法2 requests.get()发送一个Get请求3 requests.post()发送一个Post请求4 requests.head()获取HTML的头部信息5 requests.put()发送Put请求6 requests.patch()提交局部修改的请求7 requests.delete()提交删除请求最常用的方法为get()和post()分别用于发送Get请求和Post请求

4、response对象的常用属性

序号属性或方法描述1 response.status_code响应状态码2 response.content把response对象转换为二进制数据3 response.text把response对象转换为字符串数据4 response.encoding定义response对象的编码5 response.cookie获取请求后的cookie 6 response.url获取请求网址7 response.json()内置的JSON解码器8 Response.headers以字典对象存储服务器响应头,字典键不区分大小写

5、使用requests发送get请求

不带参数的get请求案例:爬取百度主页带参数的get请求案例:贴吧获取JSON数据案例:百度美女图片获取二进制数据案例:下载百度logo 5.1  不带参数的get请求# 不带参数的get请求

import requests url='http://www.baidu.com'resp = requests.get(url)# 设置响应的经编码格式resp.encoding='utf-8'cookie=resp.cookies # 获取请求后的cookie信息headers=resp.headers print('响应状态码:', resp.status_code) print('请求后的cookie:', cookie) print('获取请求的网址:', resp.url) print('响应头:', headers) print('响应内容', resp.text)----------------------------------以下为输出结果----------------------------------'''响应状态码: 200请求后的cookie: ]>获取请求的网址: http://www.baidu.com/响应头: {'Cache-Control': 'private, no-cache, no-store, proxy-revalidate, no-transform', 'Connection': 'keep-alive', 'Content-Encoding': 'gzip', 'Content-Type': 'text/html', 'Date': 'Fri, 23 Apr 2021 00:10:35 GMT', 'Last-Modified': 'Mon, 23 Jan 2017 13:28:16 GMT', 'Pragma': 'no-cache', 'Server': 'bfe/1.0.8.18', 'Set-Cookie': 'BDORZ=27315; max-age=86400; domain=.baidu.com; path=/', 'Transfer-Encoding': 'chunked'}响应内容

'''5.2 带参数的get请求5.2.1 查询参数params params,数据类型为字典作用:对URL地址中的查询参数自动进行编码拼接使用示例:resp = requests.get(url=baseurl, params=params, headers=headers)# 带参数的get请求

import requests url = 'https://tieba.baidu.com/f?'params = {'kw':'大学吧', 'pn':'3'}headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64)'}# 开始请求html = requests.get(url=url, params=params, headers=headers).text print(html)5.2.2 SSL证书认证参数 verify参数值:True(默认)| False适用网站:https类型网站但是没有经过 证书认证机构 认证的网站适用场景:当程序中抛出SSLError异常则考虑使用此参数使用示例:requests.get(url=url,headers=headers,verify=False)当verify参数设置为False时,则不会再对网站进行SSL证书认证5.2.3 设置超时时间 timeout我们可以通过timeout属性设置超时时间,一旦超过这个时间还没获得响应内容,就会提示错误。

import requests requests.get('http://github.com', timeout=0.001)

---------------------以下为输出结果(报错)---------------------Traceback (most recent call last):File "", line 1, in requests.exceptions.Timeout: HTTPConnectionPool(host='github.com', port=80): Request timed out. (timeout=0.001)5.2.4 代理IP参数 proxies 5.2.4.1 免费代理IP

语法格式:

proxies = { '协议':'协议://IP:端口号'}

示例:

当我们抓取的地址为http时,则会选择proxies中http的代理,反之为httpsimport requests

url = 'http://httpbin.org/get'headers = {'User-Agent':'Mozilla/5.0'}# 定义代理,再代理IP网站中查找免费代理IP proxies = {'http':'http://112.85.164.220:9999','https':'https://112.85.164.220:9999'}html = requests.get(url=url,proxies=proxies,headers=headers,timeout=5).text print(html)5.2.4.1 私密代理和独享代理语法格式:proxies = { '协议':'协议://用户名:密码@IP:端口号'}

示例:

5.3 获取JSON数据# 获取json数据

# 案例:百度获取宫崎骏动漫图片# 滑动页面,URL没变化,F12中的文件越来越多,说明这是动态网页# 选择XHR中的一个,复制其Request URL,粘贴给url

import requests url='https://image.baidu.com/search/acjson?tn=resultjson_com&logid=10167214135414424439&ipn=rj&ct=201326592&is=&fp=result&queryWord=%E5%AE%AB%E5%B4%8E%E9%AA%8F%E5%8A%A8%E6%BC%AB%E5%9B%BE%E7%89%87&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=&z=&ic=&hd=&latest=©right=&word=%E5%AE%AB%E5%B4%8E%E9%AA%8F%E5%8A%A8%E6%BC%AB%E5%9B%BE%E7%89%87&s=&se=&tab=&width=&height=&face=&istype=&qc=&nc=&fr=&expermode=&force=&pn=30&rn=30&gsm=1e&1619134335166='resp=requests.get(url) json_data=resp.json() print(json_data)5.4 获取二进制数据一般来说,对于非文本请求,可以以字节形式访问响应正文。

# 获取二进制数据

# 案例:保存百度图片import requests url='https://www.baidu.com/img/bd_logo1.png'resp=requests.get(url)# 存储with open('logo.png','wb') as file:# resp.content:把response对象转换为二进制数据file.write(resp.content)

6、使用requests发送post请求

语法结构requests.post(url, data=None, json=None)参数说明url:需要爬取的网站的网址data:请求数据json:json格式的数据案例:登录小说楼https://www.xslou.com/login.phpimport requests url='https://www.xslou.com/login.php'data={'username':'18600605736', 'password':'57365736', 'action':'login'}resp = requests.post(url,data) resp.encoding='gb2312'print('响应状态码:', resp.status_code) # 200 print('响应内容', resp.text) # ......

7、使用requests的session发送请求

import requests url='https://www.xslou.com/login.php'data={'username':'18600605736', 'password':'57365736', 'action':'login'}

# 使用session发送请求session = requests.session() resp=session.post(url,data=data) # 使用session发送post请求resp.encoding='gb2312'# print( resp.text) # ..登录成功....总结到此这篇关于python中requests库安装与使用的文章就介绍到这了,更多相关python requests库详解内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:Python中第三方库Requests库的高级用法详解Python requests库用法实例详解Python Requests库及用法详解Python爬虫之requests库基本介绍python下载安装requests库的简单步骤Python requests库的安装教程实例代码

相关文章