requests模拟登陆的三种方式

###获取登录后的页面三种方式:

一、实例化seesion,使用seesion发送post请求,在使用他获取登陆后的页面

import requests

session = requests.session()
post_url="http://www.renren.com/plogin.do";
post_data={"email":"2290187229@qq.com","password":"yhnujm2490@"}
headers={"User-Agent":"Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Mobile Safari/537.36"}
#使用session发送post请求,cookie保存在其中
session.post(post_url,data=post_data,headers=headers)
#在使用session进行请求登陆之后才能访问的地址
r = session.get("http://www.renren.com/970256283",headers=headers)
#保存页面
with open("renren1.html","w",encoding="utf-8") as f :
f.write(r.content.decode())

详见:04_login_renren.py文件

二、header中添加cookies健,值为cookies字符串

import requests

session = requests.session()
post_url="http://www.renren.com/plogin.do";
post_data={"email":"2290187229@qq.com","password":"yhnujm2490@"}
headers={"User-Agent":"Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Mobile Safari/537.36"}
#使用session发送post请求,cookie保存在其中
session.post(post_url,data=post_data,headers=headers)
#在使用session进行请求登陆之后才能访问的地址
r = session.get("http://www.renren.com/970256283",headers=headers)
#保存页面
with open("renren1.html","w",encoding="utf-8") as f :
f.write(r.content.decode())
详见:05_login_renren.py文件

三、在请求方法中添加cookies参数,接收字典形式的cookie,字典形式的cookie中的键是cookie的name,值是cookie的value

import requests

session = requests.session()
headers = {
"User-Agent":"Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Mobile Safari/537.36"
}
cookies="anonymid=jtwkj674xq92i8; depovince=GW; _r01_=1; JSESSIONID=abc220tCvwEeEgjJKHsNw; ick_login=def41163-f805-432b-b20e-6db8f8e44ee9; loginfrom=null; jebe_key=e392d3ef-0260-430a-8f19-5f5ffaa3fece%7C38ea40d581b68abb486e592e517d75dc%7C1554015729218%7C1%7C1554015720533; _ga=GA1.2.881890060.1554015731; _gid=GA1.2.552898793.1554015731; _de=C4918E6DB9BF3E4AFE0ED2D7064E52D56DEBB8C2103DE356; t=b3c77548e1525fc74326c8e87360d37a3; societyguester=b3c77548e1525fc74326c8e87360d37a3; id=970256283; xnsid=2d68486a; ver=7.0; wp_fold=0; jebecookies=739c2573-5918-4236-8266-38292006fbec|||||"
cookies={i.split("=")[0]:i.split("=")[0]for i in cookies.split("; ")}
print(cookies)
#在使用session进行请求登陆之后才能访问的地址
r = requests.get("http://www.renren.com/970256283/profile",headers=headers,cookies=cookies)
# #保存页面
with open("renren3.html","w",encoding="utf-8") as f :
f.write(r.content.decode())
详见:06_login_renren.py文件
详见百度网盘:链接:https://pan.baidu.com/s/13tmCu0ftpnPhWw-Mun7kBw 提取码:7idh

 

 

 

 

转载于:https://www.cnblogs.com/simpleBlue3/p/10631639.html

上一篇:后端项目部署与负载均衡


下一篇:niaobulashi-一个基于springboot shrio的权限管理系统