使用 python 请求保持登录状态 [英] staying logged in w/ python requests

查看:127
本文介绍了使用 python 请求保持登录状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在尝试使用 python 和请求登录并导航到一个页面.我很确定我正在登录,但是一旦我尝试导航到某个页面,我从该页面打印的 HTML 声明您必须登录才能看到此页面.

So I am trying to log into and navigate to a page using python and requests. I'm pretty sure I am getting logged in, but once I try to navigate to a page the HTML I print from that page states you must be logged in to see this page.

这是我的登录代码.

payload = {

    'business_user_session[email]': 'some_email',
    'business_user_session[password]': 'some_pass'
    }


with session() as c:
    r = c.post('https://my.example.com/login', data=payload)
    r2 = c.get('https://my.example.com/transactions')
    #print r.headers
    print r2.text

任何帮助或想法都会很棒!

Any help or ideas would be great!

推荐答案

以下是登录的示例,获取结果 cookie,然后使用它们发出另一个请求:

Here's an example of logging in, getting the resultant cookies, then using them to make another request:

    login_request = requests.post(ROUTE_AUTHENTICATE,
                                  data=LOGIN_CREDS,
                                  headers={"Content-Type": "application/json"})
    self.assertEqual(login_request.status_code, 200)
    self.cookies = login_request.cookies

...

    thing_request = requests.get(url, cookies=self.cookies)
    self.assertTrue(thing_request.status_code, 200)

    things = json.loads(things.text)

这篇关于使用 python 请求保持登录状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆