使用requests进行模拟登陆

Posted TAMAYURA

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用requests进行模拟登陆相关的知识,希望对你有一定的参考价值。

import re
import requests

header = {
    User-Agent: "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (Khtml, like Gecko) Chrome/22.0.1207.1 Safari/537.1",
    Referer: https://passport.lagou.com/login/login.html,
    }

session = requests.session()
r1 = session.get(https://passport.lagou.com/login/login.html,
                 headers=header
                 )
X_Anti_Forge_Token = re.findall(r"X_Anti_Forge_Token = ‘(.*?)‘", r1.text)[0]
X_Anti_Forge_Code = re.findall(r"X_Anti_Forge_Code = ‘(.*?)‘", r1.text)[0]

# X-Anit-Forge-Code:45530844
# X-Anit-Forge-Token:7f50218e-0796-46bc-9190-1fbe6790cf56
# X-Requested-With:XMLHttpRequest

header2 = header[X-Anit-Forge-Code] = X_Anti_Forge_Code
header[X-Anit-Forge-Token] = X_Anti_Forge_Token

# print(header2)

session.post(https://passport.lagou.com/login/login.json,
             headers=header,
             data={
                 isValidate: True,
                 username: 18611453110,
                 password: 70621c64832c4d4d66a47be6150b4a8e,
                 request_form_verifyCode: ‘‘,
                 submit: ‘‘,
             },
             )



r3=session.get(https://passport.lagou.com/grantServiceTicket/grant.html,
            headers={
                User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36,
                Referer: https://passport.lagou.com/login/login.html,
            }
            )

#第四步:验证
r4=session.get(https://www.lagou.com/resume/myresume.html,
               headers={
                   User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36,
               }
               )

print(18611453110 in r4.text)



r6 = session.post(https://www.lagou.com/jobs/companyAjax.json,
             headers = header,
             params = {

             },
                  data={

                  }
             )



print(r6.text)

 

以上是关于使用requests进行模拟登陆的主要内容,如果未能解决你的问题,请参考以下文章

使用requests模拟登陆github

Python模拟登陆万能法-微博|知乎

requests模拟登陆的三种方式

Python爬虫 —— 知乎之selenium模拟登陆+requests.Session()获取cookies

requests模拟登陆

requests-模拟登陆