如何使用 Python 请求登录网站,存储 cookie,然后访问网站上的另一个页面? [英] How to use Python Requests to login to website, store cookie, then access another page on the website?

查看:20
本文介绍了如何使用 Python 请求登录网站,存储 cookie,然后访问网站上的另一个页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Python 脚本登录网站,存储我收到的 cookie,然后使用相同的 cookie 访问网站的会员专区.我已经阅读了几篇关于这个主题的帖子和答案,但没有一个答案对我有用.

I'm trying to login into website using a Python script, store the cookie I receive, and then use that same cookie to access member-only parts of the website. I've read several posts and answers about this topic, but none of the answers have worked for me.

这是我尝试访问的网站登录页面的 HTML 代码.

Here is the HTML code for the website login page I'm trying to access.

<form action="/login?task=user.login" method="post">
    <fieldset>
        <table border="0" cellspacing="0" cellpadding="0">
        <tbody>
                                                        <tr>
            <td width="70" nowrap="">Username&nbsp;&nbsp;</td>
            <td width="260"><input type="text" name="username" id="username" value="" class="validate-username" size="25"/></td>
                    </tr>
                                                                                <tr>
            <td width="70" nowrap="">Password&nbsp;&nbsp;</td>
             <td width="260"><input type="password" name="password" id="password" value="" class="validate-password" size="25"/></td>
         </tr>
                                                        <tr>
             <td colspan="2"><label style="float: left;width: 70%;" for="modlgn_remember">Remember Me</label>
             <input style="float: right;width: 20%;"id="modlgn_remember" type="checkbox" name="remember" class="inputbox" value="yes"/></td>
         </tr>
         <tr>
            <td  colspan="2" width="100%"> <a href="/reset-password"> Forgot your password?</a></td>
        </tr>
        <tr>
            <td  colspan="2" width="100%"> <a href="/username-reminder">Forgot your username?</a></td>
        </tr>
        <tr>
            <td colspan="2"><button type="submit" class="button cta">Log in</button></td>
<!--                            <td colspan="1"><a href="/--><!--">Register Now</a></td>-->
        </tr>
        </tbody>
        </table>

        <input type="hidden" name="return"
               value="aHR0cHM6Ly9maWYuY29tLw=="/>
        <input type="hidden" name="3295f23066f7c6ab53c290c6c022cc4b" value="1" />                    </fieldset>
</form>

这是我自己用来尝试登录的代码.

Here is my own code that I'm using to attempt a login.

from requests import session

payload = {
     'username': 'MY_USERNAME',
     'password': 'MY_PASSWORD'
}

s = session()
s.post('https://fif.com/login?task=user.login', data=payload)

response = s.get('https://fif.com/tools/capacity')

从我读过的所有内容来看,这应该有效,但没有.我已经为此苦苦挣扎了两天,所以如果你知道答案,我会喜欢这个解决方案.

From everything I have read, this should work, but it doesn't. I've been struggling with this for two days, so if you know the answer, I would love the solution.

作为参考,这里是我看过的所有其他 StackOverflow 帖子,希望得到答案:

For reference, here are all the other StackOverflow posts I have looked at in hopes for an answer:

  1. Python 请求和持久会话
  2. 使用 Python Reqeusts 登录站点
  3. 使用python登录网站
  4. 如何使用 Python 的请求模块登录"网站?
  5. Python:请求会话登录 Cookie
  6. 如何使用 Python 登录网页并检索 cookie 以备后用?
  7. cUrl 登录然后 cUrl 下载

推荐答案

您应该发布所有需要的数据,您可以使用 bs4 解析登录页面以获取您需要的值:

You should be posting all the required data, you can use bs4 to parse the login page to get the values you need:

from requests import session
from bs4 import BeautifulSoup

data = {
    'username': 'MY_USERNAME',
    'password': 'MY_PASSWORD'
}

head = {"User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"}
with  session() as s:
    soup = BeautifulSoup(s.get("https://fif.com/login").content)
    form_data = soup.select("form[action^=/login?task] input")
    data.update({inp["name"]: inp["value"] for inp in form_data if inp["name"] not in data})
    s.post('https://fif.com/login?task=user.login', data=data, headers=head)
    resp = s.get('https://fif.com/tools/capacity')

如果您提出请求并查看 chrome 工具或 firebug,表单数据如下所示:

If you make a requests and look in chrome tools or firebug, the form data looks like:

username:foo
password:bar
return:aW5kZXgucGhwP29wdGlvbj1jb21fdXNlcnMmdmlldz1wcm9maWxl
d68a2b40daf7b6c8eaa3a2f652f7ee62:1

这篇关于如何使用 Python 请求登录网站,存储 cookie,然后访问网站上的另一个页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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