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

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

问题描述

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



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

 < form action =/ login?task = user.loginmethod =post> 
< fieldset>
< table border =0cellspacing =0cellpadding =0>
< tbody>
< tr>
< td width =70nowrap =>用户名& nbsp;< / td>
< td width =260>< input type =textname =usernameid =usernamevalue =class =validate-usernamesize =25/> ;< / td>
< / tr>
< tr>
< td width =70nowrap =>密码& nbsp;< / td>
< td width =260>< input type =passwordname =passwordid =passwordvalue =class =validate-passwordsize =25 ;< / td>
< / tr>
< tr>
< td colspan =2>< label style =float:left; width:70%; for =modlgn_remember>记住我< / label>
< input style =float:right; width:20%;id =modlgn_remembertype =checkboxname =rememberclass =inputboxvalue =yes/& / td>
< / tr>
< td colspan =2width =100%> < a href =/ reset-password>忘记密码?< / a>< / td>
< / tr>
< tr>
< td colspan =2width =100%> < a href =/ username-reminder>忘记了用户名?< / a>< / td>
< / tr>
< tr>
< td colspan =2>< button type =submitclass =button cta>登录< / button>< / td&
<! - < td colspan =1>< a href =/ - >< !->立即注册< / a>< / td& - >
< / tr>
< / tbody>
< / table>

< input type =hiddenname =return
value =aHR0cHM6Ly9maWYuY29tLw ==/>
< input type =hiddenname =3295f23066f7c6ab53c290c6c022cc4bvalue =1/> < / fieldset>
< / form>

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

 从请求导入会话

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

从我读过的一切,这应该工作,但它不。我一直在努力与这两天,所以如果你知道答案,我会喜欢的解决方案。



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


  1. Python请求和持续会话

  2. 使用Python Reqeusts登录网站

  3. 使用python登录网站

    a>

  4. 如何使用Python的请求模块登录网站?

  5. Python:请求会话登录Cookie

  6. 如何使用Python登录网页并检索cookies以备将来使用? / a>

  7. cUrl登录然后cUrl下载
  8. 解析登录页面以获取所需的值:

     从请求导入会话
    从bs4 import BeautifulSoup

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

    = {User-Agent:Mozilla / 5.0(X11; Linux x86_64)AppleWebKit / 537.36(KHTML,像Gecko)Chrome / 51.0.2704.103 Safari / 537.36}
    with session()as s:
    soup = BeautifulSoup(s.get(https://
    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


    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.

    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.

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

    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

    解决方案

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

    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天全站免登陆