美文网首页
session使用方法

session使用方法

作者: f5423d3134f0 | 来源:发表于2020-07-05 23:04 被阅读0次

    requests.sesson进行状态保持
    作用
    自动处理cookie
    场景
    连续的多次请求
    使用方法
    res = requests.sesson()
    r = res.get/post(url)

    利用session登录gethub

    import requests
    import re
    
    def login():
        # session
        session = requests.session()
    
        # headers
        headers = {
            "User - Agent":"Mozilla / 5.0(Windows NT 6.1;Win64;x64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 83.0.4103.61Safari / 537.36"
        }
        #url-获取token
        url1 = "https://github.com/login"
        #发送请求获取响应
        res_1 = session.get(url1).content.decode()
        #正则提取
        token = re.findall('name="authenticity_token" value="(.*?)" /> ',res_1)[0]
    
        # url2-登录
        url2 = "https://github.com/session"
        # 构建表格数据
        data = {
            "commit": "Sign in",
            "authenticity_token": token,
            "ga_id": "1838999590.1591763922",
            "login": "DoubleGlory",
            "password": "DoubleGlory123456",
            "webauthn - support": "supported",
            "webauthn - iuvpaa - support": "unsupported",
            "return_to":"" ,
            "required_field_d58b":"",
            "timestamp": "1593958895984",
            "timestamp_secret": "09ec3b8d6c73065b8d6ef1bafc863f90f1945392c5001bc9d6ebec453f5e6ae3"
        }
        # 发送请求登录
        session.post(url2,data=data)
        #url3-验证
        url3 = "https://github.com/DoubleGlory"
        response = session.get(url3)
        print(response.content)
    if __name__ == '__main__':
        login()
    

    相关文章

      网友评论

          本文标题:session使用方法

          本文链接:https://www.haomeiwen.com/subject/vdjbqktx.html