345243lkj 345243lkj - 2 months ago 10
Python Question

How to Login to a Page Where Logins are Optional

I'm trying to login to a webpage however, the problem--as far as I cant tell--is that my username and password aren't getting passed by

post
request.

So far I've tried:

with requests.Session() as s:
p = s.post('http://www.marinetraffic.com/en/users/ajax_user_menu?', headers=user_agent1, data=payload)
r = s.get('http://www.marinetraffic.com/en/ais/index/port_moves/all/include_anchs:yes/ship_type:7/_:3525d580eade08cfdb72083b248185a9/in_transit:yes/time_interval:1474912018_1474998300/per_page:50/port:2341/portname:MUMBAI')


Where
user-agent
is my user agent, and
payload
are my valid login credentials. With both the
post
and
get
requests I get a
200
response, however the page I see is the same as if I hadn't logged in, i.e. the results aren't filtered the way the URL indicates they should be.

How do I make sure that I'm logged in?

Answer

It is actually completely the wrong url you posted, you need to post to https://www.marinetraffic.com/en/users/ajax_login and set the correct headers:

data = [("_method", (None, "POST")), ("data[email]", (None, "you@mail.com")),
        ("data[password]", (None, "pass"))]
with requests.Session() as s:

    s.headers.update({"User-Agent": "Mozilla/5.0 (X11; Linux x86_64)",
                  "X-Requested-With": "XMLHttpRequest"})
    p = s.post("https://www.marinetraffic.com/en/users/ajax_login", files=data)
    print(p.content)
    print(s.get("http://www.marinetraffic.com/en/ajax_user_settings/get_user_settings").json())

For the record this would also work:

with requests.Session() as s:
    data = dict(email="you@mail.com", password="pass", _method="POST")
    s.headers.update({"User-Agent": "Mozilla/5.0 (X11; Linux x86_64)",
                  "X-Requested-With": "XMLHttpRequest"})
    p = s.post("https://www.marinetraffic.com/en/users/ajax_login", data=data)
    print p.content
    print(s.get("http://www.marinetraffic.com/en/ajax_user_settings/get_user_settings").json())