session方法获取登录邮箱之后的界面

Python 专栏收录该内容
23 篇文章 0 订阅

#使用requests提供的session类来请求登录之后的网站的思路
-实例化session
-先使用session发送请求,登录对网站,把cookie保存在session中
-再使用session请求登录之后才能访问的网站,session能够自动的携带登录成功时保存在其中的cookie,进行请求

import requests

session = requests.session()
post_url = "https://webmail30.189.cn/w2/"
post_data = {"email":"xxx","password":"xxx"}

headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36"}

session.post(post_url,data=post_data,headers=headers)
r = session.get("https://webmail30.189.cn/w2/logon/signOn.do",headers=headers)
with open("189.html","w",encoding="utf-8") as fp:
	fp.write(r.content.decode())

#不发送post请求,使用cookie获取登录后的页面,headers添加cookie键,值为cookie字符串

import requests

headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36",
           "Cookie":"JSESSIONID=aaaNxRREAgVzQ9OFkBzwx; apm_ct=20201110221720799; apm_sid=B7C73F297A652DEF3A4FE633EA0C0A1F; apm_uid=2D19FFBC51EEB2F8567395DA285AA8A7; apm_ip=7D045E1E828F1F471DFABD07559BB0CC1CEAEF4FE5DD0534EC4D1F73E530F882742BF3C2A4528E3A; apm_ua=46ABF0D68E56D79F1380FFAAB81ADCE5; qimo_seokeywords_92f4b1d0-eda7-11e6-9886-e964fbee51ea=; href=https%3A%2F%2Fwebmail30.189.cn%2Fw2%2F; accessId=92f4b1d0-eda7-11e6-9886-e964fbee51ea; nice_id92f4b1d0-eda7-11e6-9886-e964fbee51ea=72caa302-235f-11eb-8b38-edb1cf881315; bad_id92f4b1d0-eda7-11e6-9886-e964fbee51ea=72caa301-235f-11eb-8b38-edb1cf881315; advertId=9108c59c69e25cc1bb28dd69590a1bb7170b923c7757beb1; qimo_seosource_92f4b1d0-eda7-11e6-9886-e964fbee51ea=%E7%99%BE%E5%BA%A6%E6%90%9C%E7%B4%A2; LOGINMODE=2; 189WEB_SESSION_ID=bbbd07331bcaa5c89db7c878d65dcf95; 189ACCOUNT=CSNmO35flivNRsZgN1oiz6zbDrq+VoKW9s5vN/BAW3M=; globalValues=%7B%22isCT%22%3A%220%22%2C%22userName%22%3A%2218901065221%22%2C%22domainId%22%3A100%2C%22domainName%22%3A%22189.cn%22%2C%22mainAliasName%22%3A%22%22%2C%22aliasNameList%22%3A%5B%5D%2C%22colorId%22%3A8%2C%22puserId%22%3A%2299052359260%22%2C%22columnNum%22%3A2%2C%22sendMailAccount%22%3A%2218901065221%22%2C%22sendMailName%22%3A%22%22%2C%22bindAccount%22%3A%5B%5D%2C%22popLabelList%22%3A%5B%5D%2C%22homepage%22%3A0%2C%22addrLimit%22%3A50%2C%22sid%22%3A%22bbbd07331bcaa5c89db7c878d65dcf95%22%2C%22lastOpTime%22%3A%22Nov+10%2C+2020%2C+10%3A19%3A02+PM%22%2C%22uuserId%22%3A%22383118504340%22%2C%22safeRet%22%3A%220%22%2C%22urlSafeRet%22%3A%2299999%22%2C%22ouId%22%3A142%2C%22templateId%22%3A47%2C%22provinceId%22%3A%221%22%2C%22fullMailSearchFlag%22%3A0%2C%22isRelatedDzzd%22%3Afalse%2C%22sessionToken%22%3A%22r1allw3i%22%2C%22level%22%3A2%2C%22udDiminish%22%3A0%2C%22pwdLevel%22%3A%220%22%2C%22provinceInfo%22%3A%7B%22pwdLevelWindow%22%3Afalse%7D%2C%22newUserGuide%22%3A%5B%7B%22id%22%3A%221%22%2C%22name%22%3A%22help%22%2C%22showDate%22%3A%221573142400000%22%7D%2C%7B%22id%22%3A%222%22%2C%22name%22%3A%22delete%22%2C%22showDate%22%3A%221554979996000%22%7D%5D%2C%22smartLabelFlag%22%3A1%2C%22pop3%22%3A%221%22%2C%22imap%22%3A%221%22%2C%22encodeUserName%22%3A%22CSNmO35flivNRsZgN1oiz6zbDrq%2BVoKW9s5vN%2FBAW3M%5Cu003d%22%2C%22code%22%3A0%7D; 189SERVERLEVEL=3; pageViewNum=11"
}

r = requests.get("https://webmail30.189.cn/w2/logon/signOn.do",headers=headers)
with open("1891.html","w",encoding="utf-8") as fp:
	fp.write(r.content.decode())

#在请求方法中添加cookie参数,接受字典形式的cookie中的键是cookie的name对应的值,值是cookie的value对应的值

import requests

headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36",
           }
Cookie = "JSESSIONID=aaaNxRREAgVzQ9OFkBzwx; apm_ct=20201110221720799; apm_sid=B7C73F297A652DEF3A4FE633EA0C0A1F; apm_uid=2D19FFBC51EEB2F8567395DA285AA8A7; apm_ip=7D045E1E828F1F471DFABD07559BB0CC1CEAEF4FE5DD0534EC4D1F73E530F882742BF3C2A4528E3A; apm_ua=46ABF0D68E56D79F1380FFAAB81ADCE5; qimo_seokeywords_92f4b1d0-eda7-11e6-9886-e964fbee51ea=; href=https%3A%2F%2Fwebmail30.189.cn%2Fw2%2F; accessId=92f4b1d0-eda7-11e6-9886-e964fbee51ea; nice_id92f4b1d0-eda7-11e6-9886-e964fbee51ea=72caa302-235f-11eb-8b38-edb1cf881315; bad_id92f4b1d0-eda7-11e6-9886-e964fbee51ea=72caa301-235f-11eb-8b38-edb1cf881315; advertId=9108c59c69e25cc1bb28dd69590a1bb7170b923c7757beb1; qimo_seosource_92f4b1d0-eda7-11e6-9886-e964fbee51ea=%E7%99%BE%E5%BA%A6%E6%90%9C%E7%B4%A2; LOGINMODE=2; 189WEB_SESSION_ID=bbbd07331bcaa5c89db7c878d65dcf95; 189ACCOUNT=CSNmO35flivNRsZgN1oiz6zbDrq+VoKW9s5vN/BAW3M=; globalValues=%7B%22isCT%22%3A%220%22%2C%22userName%22%3A%2218901065221%22%2C%22domainId%22%3A100%2C%22domainName%22%3A%22189.cn%22%2C%22mainAliasName%22%3A%22%22%2C%22aliasNameList%22%3A%5B%5D%2C%22colorId%22%3A8%2C%22puserId%22%3A%2299052359260%22%2C%22columnNum%22%3A2%2C%22sendMailAccount%22%3A%2218901065221%22%2C%22sendMailName%22%3A%22%22%2C%22bindAccount%22%3A%5B%5D%2C%22popLabelList%22%3A%5B%5D%2C%22homepage%22%3A0%2C%22addrLimit%22%3A50%2C%22sid%22%3A%22bbbd07331bcaa5c89db7c878d65dcf95%22%2C%22lastOpTime%22%3A%22Nov+10%2C+2020%2C+10%3A19%3A02+PM%22%2C%22uuserId%22%3A%22383118504340%22%2C%22safeRet%22%3A%220%22%2C%22urlSafeRet%22%3A%2299999%22%2C%22ouId%22%3A142%2C%22templateId%22%3A47%2C%22provinceId%22%3A%221%22%2C%22fullMailSearchFlag%22%3A0%2C%22isRelatedDzzd%22%3Afalse%2C%22sessionToken%22%3A%22r1allw3i%22%2C%22level%22%3A2%2C%22udDiminish%22%3A0%2C%22pwdLevel%22%3A%220%22%2C%22provinceInfo%22%3A%7B%22pwdLevelWindow%22%3Afalse%7D%2C%22newUserGuide%22%3A%5B%7B%22id%22%3A%221%22%2C%22name%22%3A%22help%22%2C%22showDate%22%3A%221573142400000%22%7D%2C%7B%22id%22%3A%222%22%2C%22name%22%3A%22delete%22%2C%22showDate%22%3A%221554979996000%22%7D%5D%2C%22smartLabelFlag%22%3A1%2C%22pop3%22%3A%221%22%2C%22imap%22%3A%221%22%2C%22encodeUserName%22%3A%22CSNmO35flivNRsZgN1oiz6zbDrq%2BVoKW9s5vN%2FBAW3M%5Cu003d%22%2C%22code%22%3A0%7D; 189SERVERLEVEL=3; pageViewNum=11"
cookies = {i.split("=")[0]:i.split("=")[1] for i in Cookie.split("; ")}
print(cookies)
r = requests.get("https://webmail30.189.cn/w2/logon/signOn.do",headers=headers,cookies=cookies)
with open("1892.html","w",encoding="utf-8") as fp:
	fp.write(r.content.decode())
  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值