python模拟登陆遇到重定向_Python请求通过重定向登录

post url不正确并且缺少表单数据,还需要执行初始请求,将post发送到正确的url,然后获取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)

您可以在chrome dev tools的“网络”选项卡下看到帖子:

另外,post或get请求的默认设置是允许重定向,因此不需要在此处指定。在

您可以在登录页面源中看到表单操作:

^{pr2}$

与其硬编码我们可以从表单解析的路径,不如使用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)

现在唯一的问题是,数据大部分是使用ajax请求填充的,因此如果您想要获取数据,您需要模拟这些请求。在

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值