cookie 处理 以及模拟登陆

时间:2021-07-23 09:43:26

cookie的处理

  • 1.手动处理:
    • cookie封装到headers
  • 2.自动处理:
    • 1.获取一个session对象
    • 2.使用session对象进行请求的发送
    • 3.作用:在使用session进行请求发送过程中如果产生了cookie,则cookie会被自动存储到session对象中
    1. 使用fidder 抓包工具抓取 请求数据包

from lxml import etree
from urllib import request
url = 'http://www.renren.com/'
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
}
page_text = requests.get(url=url,headers=headers).text
#解析验证码图片
tree = etree.HTML(page_text)
code_img_url = tree.xpath('//*[@id="verifyPic_login"]/@src')[0]
request.urlretrieve(code_img_url,'./code.jpg') code_text = getCodeText('bobo328410948','bobo328410948','./code.jpg',2004) #登录
login_url = 'http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=201924831467'
data = {
"email":"**********",
"icode":code_text,
"origURL":"http://www.renren.com/home",
"domain":"renren.com",
"key_id":"1",
"captcha_type":"web_login",
"password":"436e61246c5c1c36a288c0e625b4e10f9b9c8df72b7a3f6e06a182c38d2286cb",
"rkey":"77a46f4376c93a80d35930b2cacb6a8d",
"f":"",
} #创建一个session对象
session = requests.Session()
#使用session进行请求的发送:获取cookie,且将cookie保存到session中
session.post(url=login_url,data=data,headers=headers) #获取个人主页对应的页面数据
detail_url = 'http://www.renren.com/289676607/profile'
#该次请求发送是就已经携带了cookie
page_text = session.get(url=detail_url,headers=headers).text
with open('./renren.html','w',encoding='utf-8') as fp:
fp.write(page_text)
#对某诗文网进行模拟登录
from lxml import etree
from urllib import request
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
}
session = requests.Session() #获取验证码图片
url = 'https://so.******.org/user/login.aspx?from=http://so.*****.org/user/collect.aspx'
page_text = requests.get(url=url,headers=headers).text
tree = etree.HTML(page_text)
code_img_url = 'https://so.*****.org'+tree.xpath('//*[@id="imgCode"]/@src')[0]
# request.urlretrieve(code_img_url,'./gushiwen.jpg')
img_data = session.get(url=code_img_url,headers=headers).content
with open('./gushiwen.jpg','wb') as fp:
fp.write(img_data) code_text = getCodeText('bobo328410948','bobo328410948','./gushiwen.jpg',1004) #执行登录
__VIEWSTATE = tree.xpath('//*[@id="__VIEWSTATE"]/@value')[0]
__VIEWSTATEGENERATOR = tree.xpath('//*[@id="__VIEWSTATEGENERATOR"]/@value')[0]
login_url = 'https://so.gushiwen.org/user/login.aspx?from=http%3a%2f%2fso.gushiwen.org%2fuser%2fcollect.aspx'
data = {
"__VIEWSTATE":__VIEWSTATE,
"__VIEWSTATEGENERATOR":__VIEWSTATEGENERATOR,
"from":"http://so.*****.org/user/collect.aspx",
"email":"***************",
"pwd":"**************",
"code":code_text,
"denglu":"登录",
} page_text = session.post(url=login_url,data=data,headers=headers).text with open('./gushiwen.html','w',encoding='utf-8') as fp:
fp.write(page_text)