我试图登录网页,但问题是 - 我无法告知 - 我的用户名和密码没有被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
是我的有效登录凭据。对于post
和get
请求,我得到200
响应,但是我看到的页面与我没有登录的页面相同,即结果不是&#39 ; t过滤URL指示它们应该的方式。
如何确保我已登录?
答案 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)