Python请求使用重定向登录 [英] Python requests login with redirection

查看:32
本文介绍了Python请求使用重定向登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个站点

同样,post 或 get 请求的默认设置是允许重定向,因此您无需在此处指定.

在登录页面源码中可以看到,表单动作:

我们可以从表单中解析路径,而不是硬编码路径,使用bs4:

导入请求从 bs4 导入 BeautifulSoup从 urlparse 导入 urljoin数据 = {用户":演示","passw": "演示","提交": "输入","lang": "en",动作":登录"}head = {"User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"}使用 requests.Session() 作为 c:汤 = 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)打印(c.get(urljoin(http://pro.wialon.com/",redir)).内容)

现在唯一的问题是数据主要是使用 ajax 请求填充的,因此如果您想抓取数据,则需要模拟请求.

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:

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:

<form class="login_bg_form" id="login_form" action="login_action.html" method="POST">

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.

这篇关于Python请求使用重定向登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆