使用Python登录以使用令牌保护网站

时间:2015-12-21 15:54:01

标签: python

我正在尝试使用Python脚本登录我的银行帐户,以进一步废弃我所做的交易。我在那看了很多,似乎并不复杂,但是我没有成功登录。我认为问题是当我发布我的表单时,我会错过一个由?生成的令牌?而且我不知道如何检索它。

以下是我的示例代码:

import requests

s = requests.Session()
s.headers.update({
    'User-agent': "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36",
})

url_identification = 'https://accweb.mouv.desjardins.com/identifiantunique/identification'
url_identification_process = 'https://accweb.mouv.desjardins.com/identifiantunique/identification/identificationProcess'

login_data = {
    'fnMemoriserUtilisateurActive': '0',
    'InfoPosteRaa': {},
    'OtherIdRaa': {},
    'nbrCartesMemorisees':1,
    'codeUtilisateur':xxxxxxxxxx,
    'description':{},
    '_tk':{},
    'infoPosteClient':{}
}    

r = s.get(url_identification)
r = s.post(url_identification_process, data=login_data)
print r.text

我的帖子将我重定向到url_identification网页,因为我无法获得正确的令牌。知道我怎么能找回它吗?我不知道它是如何产生的,因为它是一个银行网站,我担心这是不可能的:(

1 个答案:

答案 0 :(得分:0)

首先回答你的问题:

  1. 可以在GETting _tk的回复中找到url_identification的值,例如

    <input type="hidden" name="_tk" value="86167503-a1e1-4fdf-a661-47348671df9d" />
    
  2. infoPosteClientJavaScript函数add_deviceprint计算。您可能需要尝试模拟特定行为,或者只是给它一个常量值。

  3. url_identification_process InfoPosteRaaOtherIdRaadescription的POST请求的其他参数可以为空(至少在我的测试运行中)。

  4. 我没有该银行的帐户进行测试,因此我担心无法检查是否有进一步的请求。
    一条建议:我认为Python能够执行此任务,事实上我个人做了许多类似的事情(使用Python模拟XHR或其他复杂事物)。但我想使用基于JavaScript的解决方案(例如PhantomJS)可能更适合您的具体情况。