How can I log in to morningstar.com without using a headless browser such as selenium?

前端 未结 2 2270
野性不改
野性不改 2020-12-18 13:10

I read the answer to the question: \"How to “log in” to a website using Python\'s Requests module?\"

The answer reads: \"Firstly check the source of the login form t

相关标签:
2条回答
  • 2020-12-18 13:54

    As seen the code, the username input field is:

    <input id="uim-uEmail-input" name="uEmail" placeholder="E-mail Address" data-msat="formField-inputemailuEmail-login" type="email">
    

    the password input field is:

    <input id="uim-uPassword-input" name="uPassword" placeholder="Password" data-msat="formField-inputpassworduPassword-login" type="password">
    

    The name is listed for both in each line after name=:

    Username: "uEmail" Password: "uPassword"

    0 讨论(0)
  • 2020-12-18 14:13

    This was kind of hard, i had to use an intercepting proxy, but here it is:

    import requests
    
    s = requests.session()
    auth_url = 'https://sso.morningstar.com/sso/json/msusers/authenticate'
    login_url = 'https://www.morningstar.com/api/v2/user/login'
    username = 'username'
    password = 'password'
    
    headers = {
        'Access-Control-Request-Method': 'POST',
        'Access-Control-Request-Headers': 'content-type,x-openam-password,x-openam-username',
        'Origin': 'https://www.morningstar.com'
    }
    s.options(auth_url, headers=headers)
    
    headers = {
        'Referer': 'https://www.morningstar.com/members/login.html',
        'Content-Type': 'application/json',
        'X-OpenAM-Username': username,
        'X-OpenAM-Password': password,
        'Origin': 'https://www.morningstar.com',
    }
    s.post(auth_url, headers=headers)
    
    data = {"productCode":"DOT_COM","rememberMe":False}
    r = s.post(login_url, json=data)
    
    print(s.cookies)
    print(r.json())
    

    By now you should have an authenticated session. You should see a bunch of cookies in s.cookies and some basic info about your account in r.json().


    The site changed the login mechanism (and probably their entire CMS), so the above code doesn't work any more. The new login process involves one POST and one PATCH request to /umapi/v1/sessions, then a GET request to /umapi/v1/users.

    import requests
    
    sessions_url = 'https://www.morningstar.com/umapi/v1/sessions'
    users_url = 'https://www.morningstar.com/umapi/v1/users'
    
    userName = 'my email'
    password = 'my pwd'
    data = {'userName':userName,'password':password}
    
    with requests.session() as s:
        r = s.post(sessions_url, json=data)
        # The response should be 200 if creds are valid, 401 if not
        assert r.status_code == 200
        s.patch(sessions_url)
        r = s.get(users_url)
        #print(r.json()) # contains account details
    

    The URLs and other required values, such as POST data, can be obtained from the developer console (Ctrl+Shift+I) of a web-browser, under the Network tab.

    0 讨论(0)
提交回复
热议问题