requests模块快速使用

#介绍:使用requests可以模拟浏览器的请求,比起之前用到的urllib(内置模块),requests模块的api更加便捷(本质就是封装了urllib3)

# 注意:requests库发送请求将网页内容下载下来以后,并不会执行js代码,这需要我们自己分析目标站点然后发起新的request请求
	-混合项目--->拿回来的页面---带数据
    -分离项目--->拿回来的页面-->数据是空的-->再分析接口--->再发请求才能拿到真正的数据

    -看到一个页面有数据--->用requests发送请求--->拿回来的,可能跟看到的不一样
    
    
# 安装:pip install requests
# 快速使用


import requests
res=requests.get('https://www.cnblogs.com/') # res中会有:http响应  响应头的,响应体的
print(res.text) # 响应体的文本字符串(可能会乱码)

携带请求参数

# 2  get请求携带请求参数
# 方式一:直接拼在后面
res=requests.get('https://www.cnblogs.com/?ordering=-id&search=课程')
# 方式二:使用params参数
res=requests.get('https://www.cnblogs.com/',params={'ordering':'-id','search':'课程'})
print(res.text)

url 编码和解码

# %E7%BE%8E%E5%A5%B3  不是乱码--->url编码了
# url解码
res=parse.unquote('%E7%BE%8E%E5%A5%B3')
print(res)
# url编码
res=parse.quote('刷币')  # %E5%88%B7%E5%B8%81
print(res)

携带请求头

# 4 携带请求头
# 请求头中可能有的:User-Agent,referer,cookie,Host
header = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36'
}
res = requests.get('https://www.sogou.com/web?query=%E7%BE%8E%E5%A5%B3', headers=header)
# res=requests.request('get','url',headers=header)
print(res.text)

发送post请求

# 5 发送post请求
# 5.1 请求体中:两种方式:data={}--->编码格式 urlencoded--->key=value&key=value      json={}--->编码格式是json
res=requests.post('url')


# 6 模拟登录
data = {
    'username': '616564099@qq.com',
    'password': 'lqz123',
    'captcha': 'xxxx',
    'ref': 'http://www.aa7a.cn/',
    'act': 'act_login',
}
res = requests.post('http://www.aa7a.cn/user.php', data=data)
print(res.text)

携带cookie

# 7 请求携带cookie  1 请求头中    2 带在cookie参数中
方式一:请求头中携带
header = {
    'Cookie': 'cto_bundle=_DpLll9WcDY3UCUyRlBnWDZtZEp5MVFZZHVHeExtdmtwNjVVYkh6SUglMkZoVmFaSWFtc1JrZWo0aVRURzdOeUhTUHpTekslMkJ0RjRnOXhqOWhXM0piM0hXVFlVVk9MUGpNWFdCS0dveFBFSEhHVmJRV01uVWZ1bjloTU9YY2VwQWF2NWxFQWhkZnM5TERxMVlUUkt4WUo2dWR4QkZOMmclM0QlM0Q; _jzqx=1.1691985966.1691985966.1.jzqsr=aa7a%2Ecn|jzqct=/.-; ECS_ID=a9bd0486ba5b0f2f65637f64719d72e75add4995; ECS[visit_times]=8; _jzqa=1.2593057364546894000.1672632545.1691985966.1698896326.9; _jzqc=1; _jzqy=1.1672632545.1698896326.1.jzqsr=baidu.-; _jzqckmp=1; Hm_lvt_c29657ca36c6c88e02fed9a397826038=1698896327; mediav=%7B%22eid%22%3A%22179539%22%2C%22ep%22%3A%22%22%2C%22vid%22%3A%22%22%2C%22ctn%22%3A%22%22%2C%22vvid%22%3A%22%22%2C%22_mvnf%22%3A1%2C%22_mvctn%22%3A0%2C%22_mvck%22%3A1%2C%22_refnf%22%3A0%7D; __xsptplus422=422.9.1698896372.1698896372.1%234%7C%7C%7C%7C%7C%23%23%23; Qs_lvt_201322=1686282645%2C1691979017%2C1691979065%2C1698896326%2C1698896643; __xsptplusUT_422=1; _qzjc=1; ECS[username]=616564099%40qq.com; ECS[user_id]=61399; ECS[password]=4a5e6ce9d1aba9de9b31abdf303bbdc2; _qzjb=1.1698896326296.7.0.0.0; _qzjto=7.1.0; _qzja=1.1154697895.1672632545402.1691985966161.1698896326296.1698896649094.1698896661921.616564099%2540qq_com.1.0.23.8; Hm_lpvt_c29657ca36c6c88e02fed9a397826038=1698896662; _jzqb=1.18.10.1698896326.1; Qs_pv_201322=1719398001187156200%2C1811149594340033000%2C1691949923920640300%2C4523589869790951000%2C2247541305320030700'
}
res = requests.get('http://www.aa7a.cn/',headers=header)
print('616564099@qq.com' in res.text)  # 没有携带cookie 访问

案例二:自动点赞
data = {
    'linkId': '40480287'
}
header = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36',
    'Cookie': 'deviceId=web.eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJqaWQiOiIwNGZlMmMxYy00Mjc4LTRlMzMtYjM5OS0zNTI0NTNiNzQ3ZDkiLCJleHBpcmUiOiIxNjgxNDQyNDI2ODUxIn0.k41tJ-jL5Y6TO4BabrtzUv8N35eLrfqfdvBdXEmtqxE; __snaker__id=kNv5rzTfGa3xsaqh; YD00000980905869%3AWM_TID=slUolCNNTm5FRQRAEVaBfKlPbFQL7kFG; Hm_lvt_03b2668f8e8699e91d479d62bc7630f1=1698896819; gdxidpyhxdE=4hhv3p0G9z1GUt%2BbmquM7o8uxAnU21e5Xv0hEnOwniLE617vQyijd%2FRfrVM3c8u%2Bb%2BSzE%2B612mU4JXDi6SwQC6tAIHEf9RPVR%2FZQB9AH0V4gazYRMoMlmhHeYBgfjGfe%2BhbYbX3x%2BdKE2dQUo1tJuYMCZr%2B8QdQapLAChI%5CbpM%2BhkIne%3A1698897721336; YD00000980905869%3AWM_NI=817ayMhIabFSD%2BFdoaOokCH5%2FLBINb0apxw2XgvsY2zLJrWi0bgIB1eE%2FAPrdZIt4NVgofeOgPy92Y73HLGf70hYzYHH%2BKz%2FlrsYT9j4KFUbo6Vx4OY6R6o0nZ7HJYrTYm0%3D; YD00000980905869%3AWM_NIKE=9ca17ae2e6ffcda170e2e6eeccea7c97bff99ad04eb3bc8bb6c44f839a8e83d463b889f8b9c25ebc868ad5f52af0fea7c3b92afceff982fc47ac97af83c55988f58a85ee6b8db1a5a4cb3fed948eaacd6af4bfc099f63a9cb9ba94dc3aba948693d663f4bea9aac13ba79be1a2d37aa8868795b74696b9a187e845a590b7dab153f5ed88adc66ef5f0a8baf9538c98e5acf925b0aebdbad733aaec85a7d86aadea9a99b7459bea9ed7ee3cae86f9a9c953b28c978eb337e2a3; token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJqaWQiOiJjZHVfNTMyMDcwNzg0NjAiLCJleHBpcmUiOiIxNzAxNDg4ODM3Nzg0In0.3zOo3zk8OEO2kmYOX2ExY_oc6wm3eeM0y9VUyA4er-w; Hm_lpvt_03b2668f8e8699e91d479d62bc7630f1=1698896838'
}

res = requests.post('https://dig.chouti.com/link/vote', data=data, headers=header)
print(res.text)


# 方式二:使用cookie参数
字符串转字典:
from utils import cookie_str_to_cookie_dict
s = 'cto_bundle=_DpLll9WcDY3UCUyRlBnWDZtZEp5MVFZZHVHeExtdmtwNjVVYkh6SUglMkZoVmFaSWFtc1JrZWo0aVRURzdOeUhTUHpTekslMkJ0RjRnOXhqOWhXM0piM0hXVFlVVk9MUGpNWFdCS0dveFBFSEhHVmJRV01uVWZ1bjloTU9YY2VwQWF2NWxFQWhkZnM5TERxMVlUUkt4WUo2dWR4QkZOMmclM0QlM0Q; _jzqx=1.1691985966.1691985966.1.jzqsr=aa7a%2Ecn|jzqct=/.-; ECS_ID=a9bd0486ba5b0f2f65637f64719d72e75add4995; ECS[visit_times]=8; _jzqa=1.2593057364546894000.1672632545.1691985966.1698896326.9; _jzqc=1; _jzqy=1.1672632545.1698896326.1.jzqsr=baidu.-; _jzqckmp=1; Hm_lvt_c29657ca36c6c88e02fed9a397826038=1698896327; mediav=%7B%22eid%22%3A%22179539%22%2C%22ep%22%3A%22%22%2C%22vid%22%3A%22%22%2C%22ctn%22%3A%22%22%2C%22vvid%22%3A%22%22%2C%22_mvnf%22%3A1%2C%22_mvctn%22%3A0%2C%22_mvck%22%3A1%2C%22_refnf%22%3A0%7D; __xsptplus422=422.9.1698896372.1698896372.1%234%7C%7C%7C%7C%7C%23%23%23; Qs_lvt_201322=1686282645%2C1691979017%2C1691979065%2C1698896326%2C1698896643; __xsptplusUT_422=1; _qzjc=1; ECS[username]=616564099%40qq.com; ECS[user_id]=61399; ECS[password]=4a5e6ce9d1aba9de9b31abdf303bbdc2; _qzjb=1.1698896326296.7.0.0.0; _qzjto=7.1.0; _qzja=1.1154697895.1672632545402.1691985966161.1698896326296.1698896649094.1698896661921.616564099%2540qq_com.1.0.23.8; Hm_lpvt_c29657ca36c6c88e02fed9a397826038=1698896662; _jzqb=1.18.10.1698896326.1; Qs_pv_201322=1719398001187156200%2C1811149594340033000%2C1691949923920640300%2C4523589869790951000%2C2247541305320030700'
res = requests.get('http://www.aa7a.cn/', cookies=cookie_str_to_cookie_dict(s))
print('616564099@qq.com' in res.text)  # 没有携带cookie 访问


# 登录成功--->响应头中有cookie
data = {
    'username': '616564099@qq.com',
    'password': 'lqz123',
    'captcha': 'xxxx',
    'ref': 'http://www.aa7a.cn/',
    'act': 'act_login',
}
res = requests.post('http://www.aa7a.cn/user.php', data=data)
print(res.text)
print(type(res.cookies.get_dict())) # 登录成功的cookie,RequestsCookieJar  本质就是字典
from requests.cookies import RequestsCookieJar


# 拿到登录后的cookie,再向首页发送请求
res = requests.get('http://www.aa7a.cn/', cookies=res.cookies)
print('616564099@qq.com' in res.text)  # 没有携带cookie 访问
#### 8 session 的使用--->原来每次都需要手动携带cookie
from requests import Session
session=Session()
data = {
    'username': '616564099@qq.com',
    'password': 'lqz123',
    'captcha': 'xxxx',
    'ref': 'http://www.aa7a.cn/',
    'act': 'act_login',
}
res = session.post('http://www.aa7a.cn/user.php', data=data)
res = session.get('http://www.aa7a.cn/')  #  可以自动处理cookie,不需要手动携带
print('616564099@qq.com' in res.text)

响应对象

####  9  响应对象
'''  http请求--->python中--->变成了俩对象--->request对象  response对象
后端:django :
    request--->http请求被包装成了python的对象
    四件套---->http的响应被包装成了response
爬虫:
    request对象--->request.get --- request.get--->http请求
    response对象--->返回的数据--->http响应包装成了response对象
    
后端和前端所叫的 request和response虽然不是同一个类的对象,但是他们的内容基本一致--->本质都是http请求和响应

'''
# respone = requests.get('http://www.aa7a.cn/')
#
# print(type(respone))# 类的属性跟方法
# from requests.models import Response
# print(respone.text) # 响应体转成了字符串
# print(respone.content) # 响应体的二进制
# print(respone.status_code) # 响应状态码
# print(respone.headers) # 响应头
# print(respone.cookies)# 响应cookie
# print(respone.cookies.get_dict())
# print(respone.cookies.items()) # 响应cookie--->转成key-value形式
#
# print(respone.url) # 请求地址
# print(respone.history) # 了解:访问过的地址  针对于  重定向的情况,才会有值
#
# print(respone.encoding) # 响应编码


# 下载一个视频
respone = requests.get('https://img3.chouti.com/CHOUTI_231102_8B10E74FBE2646748DE951D4EAB1F1E7.jpg')
# print(respone.content)
# 保存到本地-->就是这张图片
with open('致命诱惑.jpg', 'wb') as f:
    # f.write(respone.content)
    # for line in respone.iter_content(1024):
    for line in respone.iter_content():
        f.write(line)

高级用法

## 10 高级用法:
# 1 关于https报错问题
'''
以后访问https地址时,可以会报错,访问https需要携带证书--->没带就会报错
不携带证书--->不报错
respone=requests.get('https://www.12306.cn',verify=False) #不验证证书,报警告,返回200
'''
# import urllib3
# urllib3.disable_warnings()
# respone=requests.get('https://www.12306.cn',verify=False)
# print(respone.text)


# 2 超时设置
# import requests
# respone=requests.get('https://www.baidu.com',timeout=0.0001)

# 3  认证设置 --->不需要知道了,现在没有这种了--->不是前端认证

# 4 异常处理

# 5 上传文件
# import requests
# 
# files = {'file': open('a.jpg', 'rb')}
# respone = requests.post('http://httpbin.org/post', files=files)
# print(respone.status_code)


# 后端存储  request.FILES.get('file')

posted on 2024-01-02 11:24  Way*yy  阅读(6)  评论(0编辑  收藏  举报