python重定向_Python请求重定向登录

1586010002-jmsa.png

Here is a site http://pro.wialon.com/ where I want to login with python requests module. Login and pass are demo.

import requests

with requests.Session()as c:

url = 'http://pro.wialon.com/'

payload = dict(user='demo',

passw='demo',

login_action='login')

r = c.post(url, data=payload, allow_redirects=True)

print(r.text)

Frankly, I want to get report (at the report tab) as response. But I cant figure out how to log in.

解决方案

The post url is incorrect and you are missing form data, you need to also do an initial request, post to the correct url and then get http://pro.wialon.com/service.html:

data = {"user": "demo",

"passw": "demo",

"submit": "Enter",

"lang": "en",

"action": "login"}

head = {"User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"}

with requests.Session() as c:

c.get('http://pro.wialon.com/')

url = 'http://pro.wialon.com/login_action.html'

c.post(url, data=data, headers=head)

print(c.get("http://pro.wialon.com/service.html").content)

You can see the post in chrome dev tools under the network tab:

5I58s.png

Also the default for post or get requests is to allow redirects so you don't need to specify it here.

You can see in the login page source, the form action:

Instead of hard coding the path we can parse it from the form, use bs4:

import requests

from bs4 import BeautifulSoup

from urlparse import urljoin

data = {"user": "demo",

"passw": "demo",

"submit": "Enter",

"lang": "en",

"action": "login"}

head = {"User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"}

with requests.Session()as c:

soup = BeautifulSoup(c.get('http://pro.wialon.com/').content)

redir = soup.select_one("#login_form")["action"]

url = 'http://pro.wialon.com/login_action.html'

c.post(url, data=data, headers=head)

print(c.get(urljoin("http://pro.wialon.com/", redir)).content)

The only problem now is the data is mostly populated using ajax requests so if you want to scrape data you will need to mimic the requests.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值