如何使用Python和请求登录网站帐户

时间:2018-09-29 16:00:34

标签: python authentication redirect python-requests

我正在尝试使用Python 3和“请求”模块登录2个网站帐户。

第一个网站是:https://www.amundi-ee.com/psf/#login,我需要在https://www.amundi-ee.com/psf/#avoirs之后访问此页面上的信息
第二个网站是:https://zone.mfgl.com/pensions/burohappold/index.asp(登录后,有一个重定向到https://zone.mfgl.com/pensions/burohappold/login2.asp的问题,可在4/5个选项中选择另一个额外的随机安全性问题,例如出生地或第一个宠物的名字),在我需要在此页面上获取信息:https://zone.mfgl.com/pensions/burohappold/statement-page.asp

此想法是检索个人帐户信息以在另一个脚本中对其进行处理。 我知道如何使用请求(在基本级别上),但是我在这2个特定的网站上苦苦挣扎(我相信,因为必须首先通过单击来输入密码的方式,以及由于在第二)。

import requests
import logging

logging.basicConfig(level=logging.DEBUG)

URL1 = 'https://www.amundi-ee.com/psf/#login'
URL2 = 'https://www.amundi-ee.com/psf/#'
URL3 = 'https://zone.mfgl.com/pensions/burohappold/index.asp'
URL4 = 'https://zone.mfgl.com/pensions/burohappold/login2.asp'
URL5 = 'https://zone.mfgl.com/pensions/burohappold/statement-page.asp'

payload1 = {
    'username' : '000000',
    'password' : '000000'
}
payload2 = {
    'username' : '000000',
    'password' : '000000'
}
sub_payload1 = {
    'password' : 'aaaaaa'
}
sub_payload2 = {
    'password' : 'bbbbbb'
}
sub_payload3 = {
    'password' : 'cccccc'
}

headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36'}


with requests.Session() as c:
    c.post(URL1, headers=headers, data=payload1)
    r = c.get(URL2, headers=headers)
    print(r.text)

with requests.Session() as d:
    d.post(URL3, headers=headers, data=payload2)
    #How to manage the redirection here to URL4?
    #How to test which extra security question is supplied and submit corresponding sub_payload?
    s = d.get(URL5, headers=headers)
    print(s.text)

对于第二个网站,老实说,我不知道如何处理重定向和问题测试...

有效载荷“用户名” /“密码”是否必须与这些网站表单中的特定名称匹配?

感谢您的帮助或指导(我是初学者)。

1 个答案:

答案 0 :(得分:0)

因此,如果有人感兴趣,我设法使它适用于第二个网站,代码下方。有一个隐藏的表单域。 我将为第一个网站打开一个新主题。

import requests, time
from lxml import html

LOGIN = 'https://zone.mfgl.com/pensions/burohappold/index.asp'
LOGIN2 = 'https://zone.mfgl.com/pensions/burohappold/login2.asp'
PROTECTED_PAGE = 'https://zone.mfgl.com/pensions/burohappold/statement-page.asp'
payload = {
    'username': 'username',
    'password': 'password',
    'action': 'login'
}
s_payload1 = {
    'answer': 'answer',
    'correctanswer': 'answer',
    'action': 'submit'
}
s_payload2 = {
    'answer': 'answer',
    'correctanswer': 'answer',
    'action': 'submit'
}
s_payload3 = {
    'answer': 'answer',
    'correctanswer': 'answer',
    'action': 'submit'
}

with requests.session() as s:
    s.post(LOGIN, data=payload)
    time.sleep(2)
    r = s.get(LOGIN2)
    #2nd login question test to submit appropriate sub_payload
    if 'XXXX' in r.text:
        s.post(LOGIN2, data=s_payload1)
    if 'YYYY' in r.text:
        s.post(LOGIN2, data=s_payload2)
    if 'ZZZZ' in r.text:
        s.post(LOGIN2, data=s_payload3)
    f = s.get(PROTECTED_PAGE)

tree = html.fromstring(f.content)
a = tree.xpath('//td/text()')
index = a.index("Total")
Funds = a[32]
print(Funds)