如何使用Python请求登录网站,存储cookie,然后访问网站上的另一个页面?

时间:2016-06-29 21:58:38

标签: python python-2.7 cookies python-requests

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

以下是我正在尝试访问的网站登录页面的HTML代码。

<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>

这是我自己用于尝试登录的代码。

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')

从我读过的所有内容中,这应该有效,但事实并非如此。我已经在这两天苦苦挣扎,所以如果你知道答案,我会很喜欢这个解决方案。

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

  1. Python Requests and Persistent Sessions
  2. Logging into a site using Python Reqeusts
  3. Login to website using python
  4. How to “log in” to a website using Python's Requests module?
  5. Python: Requests Session Login Cookies
  6. How to use Python to login to a webpage and retrieve cookies for later usage?
  7. cUrl Login then cUrl Download

1 个答案:

答案 0 :(得分:1)

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

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,则表单数据如下所示:

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