网站首页 > 文章精选 正文
#爬虫##python##requests##每天学python##程序员#
一、安装
- pip快速安装
pip install requests
二、使用方法
1)导入模块
import requests
2)发送请求
- 示例get请求:以百度搜索get请求为例
import requests
# 参数直接拼接到url的get请求
r1 = requests.get('https://www.baidu.com/s?wd=hello%20world')
# 参数放到params的get请求(两种get请求方法最终发送的url都是https://www.baidu.com/s?wd=hello%20world)
r2 = requests.get(url='https://www.baidu.com/s', params={'wd': 'hello%20world'})
- 常用请求方式
# GET请求
requests.get('https://code_space/get')
# POST请求
requests.post('https://code_space/post')
# PUT请求
requests.put('https://code_space/put')
# DELETE请求
requests.delete('https://code_space/delete')
3)定制头和cookie信息
cookie= {'key': 'value'}
header = {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-CN,zh;q=0.9",
"Connection": "keep-alive",
"Content-Type": "application/json",
"Host": "ug.baidu.com",
"Origin": "https://www.baidu.com",
"Referer": "https://www.baidu.com/s?ie=UTF-8&wd=hello%20world",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36"
}
r = requests.get('https://www.baidu.com/s?wd=hello%20world',headers=header,cookies=cookie)
4)获取响应
r.encoding # 获取当前的编码
r.encoding = 'utf-8' # 设置编码
r.text # 以encoding解析返回内容。字符串方式的响应体,会自动根据响应头部的字符编码进行解码。
r.content # 以字节形式(二进制)返回。字节方式的响应体,会自动为你解码 gzip 和 deflate 压缩。
r.headers # 以字典对象存储服务器响应头,但是这个字典比较特殊,字典键不区分大小写,若键不存在则返回None
r.status_code # 响应状态码
r.raw # 返回原始响应体,也就是 urllib 的 response 对象,使用 r.raw.read()
r.ok # 查看r.ok的布尔值便可以知道是否登陆成功
#*特殊方法*#
r.json() #Requests中内置的JSON解码器,以json形式返回,前提返回的内容确保是json格式的,不然解析出错会抛异常
r.raise_for_status() #失败请求(非200响应)抛出异常
- 使用requests方法后,会返回一个response对象,其存储了服务器响应的内容,如上实例中已经提到的 r.text、r.status_code……
- 获取文本方式的响应体实例:当你访问 r.text 之时,会使用其响应的文本编码进行解码,并且你可以修改其编码让 r.text 使用自定义的编码进行解码。
三、测试demo
# -*- coding: utf-8 -*-
"""
@Time : 2022/1/16 16:15
@Auth : 技术空间
@File :requests_demo.py
@IDE :PyCharm
@Motto:技术总是要日积月累的
"""
import requests
if __name__ == '__main__':
r1 = requests.get('https://www.baidu.com/s?wd=hello%20world')
cookie = {'key': 'value'}
header = {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-CN,zh;q=0.9",
"Connection": "keep-alive",
"Content-Type": "application/json",
"Host": "ug.baidu.com",
"Origin": "https://www.baidu.com",
"Referer": "https://www.baidu.com/s?ie=UTF-8&wd=hello%20world",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36"
}
r2 = requests.get('https://www.baidu.com/s?wd=hello%20world', headers=header, cookies=cookie)
print("r1的status_code-->" + str(r1.status_code))
print(r1.text)
print("r2的status_code-->" + str(r1.status_code))
print(r2.text)
四、其它拓展
关于requests的post方法使用我将会在下一篇中讲述如何使用以及注意细节。
关注我,坚持每日积累一个技巧,长期坚持,我们将会不断进步。
猜你喜欢
- 2024-12-23 python网络爬虫:批量爬取图片 python批量爬取图片并保存
- 2024-12-23 10w qps缓存数据库——Redis redis缓存数据量多大开始性能下降
- 2024-12-23 Python File(文件) 常用场景 python中file.write
- 2024-12-23 「JS 逆向百例」某网站加速乐 Cookie 混淆逆向详解
- 2024-12-23 「JS 逆向百例」猿人学web比赛第五题:js 混淆 - 乱码增强,详细剖析
- 2024-12-23 Python 操作mysql实现事务处理 python+操作mysql实现事务处理功能
- 2024-12-23 python 大量乱序文件如何合并成有序的
- 2024-12-23 如何使用 PyScript 在 Web 浏览器上轻松运行
- 2024-12-23 Selenium4.0+Python3系列(四) - 常见元素操作(含鼠标键盘事件)
- 2024-12-23 Selenium4+Python3系列(六) - 强制等待、隐式等待、显式等待
- 最近发表
- 标签列表
-
- newcoder (56)
- 字符串的长度是指 (45)
- drawcontours()参数说明 (60)
- unsignedshortint (59)
- postman并发请求 (47)
- python列表删除 (50)
- 左程云什么水平 (56)
- 计算机网络的拓扑结构是指() (45)
- 稳压管的稳压区是工作在什么区 (45)
- 编程题 (64)
- postgresql默认端口 (66)
- 数据库的概念模型独立于 (48)
- 产生系统死锁的原因可能是由于 (51)
- 数据库中只存放视图的 (62)
- 在vi中退出不保存的命令是 (53)
- 哪个命令可以将普通用户转换成超级用户 (49)
- noscript标签的作用 (48)
- 联合利华网申 (49)
- swagger和postman (46)
- 结构化程序设计主要强调 (53)
- 172.1 (57)
- apipostwebsocket (47)
- 唯品会后台 (61)
- 简历助手 (56)
- offshow (61)