Python请求重定向登录

编程入门 行业动态 更新时间:2024-10-12 16:24:14
本文介绍了Python请求重定向登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是一个我要使用python登录的网站 pro.wialon/请求模块.登录和传递是演示.

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

import requests with requests.Session()as c: url = 'pro.wialon/' 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.

推荐答案

发布网址不正确,并且您缺少表单数据,还需要执行初始请求,发布到正确的网址,然后获取pro.wialon/service.html:

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 pro.wialon/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('pro.wialon/') url = 'pro.wialon/login_action.html' c.post(url, data=data, headers=head) print(c.get("pro.wialon/service.html").content)

您可以在网络标签下的chrome开发工具中查看该帖子:

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">

使用 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('pro.wialon/').content) redir = soup.select_one("#login_form")["action"] url = 'pro.wialon/login_action.html' c.post(url, data=data, headers=head) print(c.get(urljoin("pro.wialon/", redir)).content)

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

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请求重定向登录

本文发布于:2023-07-14 20:03:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1106703.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重定向   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!