通过POST使用请求库进行python身份验证 [英] python authentication with requests library via POST

查看:73
本文介绍了通过POST使用请求库进行python身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了几个类似的主题..我尝试遵循其他示例,但是我仍然陷于茫茫之中.我具有python编程的基本技能,对http协议的了解很少,我的两个目标是:-通过请求库对网站进行完全身份验证-会话处于活动状态时,登录后从网站获取数据

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

这是代码:

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!

这是页面中的html代码:

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 

推荐答案

如果您查看发布的内容:

If you look at what gets posted:

您看到您需要 redirect_url site ,可以使用

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.

这篇关于通过POST使用请求库进行python身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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