python authentication with requests library via POST

那年仲夏 提交于 2021-02-19 08:16:14

问题


I read several similar topic.. I try to follow the others examples but I'm still stuck in the middle of nowhere.. I have basic skills of python programming and little knowledge about http protocol, my two goals are: -succesfull authentication to a website via requests library -fetch data from the website after the login while the session is active

This is the code:

import requests

targetws = 'https://secure.advfn.com/login/secure'

s = requests.session()

payload_data = {'login_username': 'xxx', 'login_password': 'yyy'}

response = s.post(targetws, data=payload_data)


url='https://it.advfn.com/mercati/BIT/generali-G/ordini'

result = s.get(url) 

print result.content

But I always get no success with log in.. Maybe I miss some value in post data like submit action or else? Any help will be appreciated, best regards!

Here the html code from the page:

form action="https://secure.advfn.com/login/secure" id="login_form" name="login_form" method="POST" target="">

        <input type="hidden" value="aHR0cDovL2l0LmFkdmZuLmNvbQ==" name="redirect_url" id="redirect_url">
        <input type="hidden" value="it" name="site" id="site">

        <div class="fields"><label for="login_username">Username</label> 
            <input type="text" tabindex="1" class="text ui-widget-content" value =""
            id="login_username" name="login_username" maxlength="64">
        </div>

        <div class="fields"><label for="login_password">Password</label> 
            <input tabindex="2" type="password" class="text ui-widget-content" value="" id="login_password" name="login_password" maxlength="16">
        </div>
                <div class="fields">
                    <strong><a href="/common/account/password/request">Se ti sei dimenticato la tua password</a></strong> &nbsp;
                    <input  class="button"  tabindex="3" type="submit"   value="Accedi" id="login_submit">
                </div>
    </form 

回答1:


If you look at what gets posted:

You see you need the redirect_url and site which you can parse from the input in the source with bs4:

import requests
from bs4 import BeautifulSoup

data = {"redirect_url": "",
        "site": "uk",
        "login_username": "foo",
        "login_password": "bar"}

with requests.Session() as s:
    log = "https://secure.advfn.com/login/secure"
    r = s.get("http://uk.advfn.com/")
    soup = BeautifulSoup(r.content)
    redirect_url = soup.select_one("#redirect_url")["value"]
    site =  soup.select_one("#site")["value"]
    data["redirect_url"] = redirect_url
    p = s.post(log, data=data)
    print(p.content)
    print(s.get('https://it.advfn.com/mercati/BIT/generali-G/ordini').content)

Once you do that you will be successfully logged in.



来源:https://stackoverflow.com/questions/37816565/python-authentication-with-requests-library-via-post

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!