如何登录登录是可选的页面

时间:2016-09-27 18:16:15

标签: python python-requests

我试图登录网页,但问题是 - 我无法告知 - 我的用户名和密码没有被post请求传递。

到目前为止,我已尝试过:

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')

user-agent是我的用户代理,payload是我的有效登录凭据。对于postget请求,我得到200响应,但是我看到的页面与我没有登录的页面相同,即结果不是&#39 ; t过滤URL指示它们应该的方式。

如何确保我已登录?

2 个答案:

答案 0 :(得分:1)

这实际上是您发布的错误网址,您需要发布到 https://www.marinetraffic.com/en/users/ajax_login 并设置正确的标题:

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())

对于记录,这也有效:

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())

答案 1 :(得分:-1)

试试这个:

payload={
    '_method':'POST',
    'email':your email,
    'password':your passwd,
    'is_ajax':True
}

p = s.post('http://www.marinetraffic.com/en/users/ajax_login', headers=user_agent1, data=payload)