简体   繁体   English

Python请求:检查登录是否成功

[英]Python Requests: Check if Login was successful

I've looked all over the place for the solution I'm looking for but just can't find it. 我到处寻找所需的解决方案,但找不到。 Basically, I'm developing a tool which takes a list of URLs from a text document, logs into them with your username/password, and returns which ones work. 基本上,我正在开发一个工具,该工具将从文本文档中获取URL列表,并使用您的用户名/密码登录到它们,然后返回有效的URL。

I have the login figured out and all, but how do I actually return if the login works? 我已经确定了所有登录名,但是如果登录名有效,我该如何实际返回? Sites entered on the list won't necessarily use cookies and will be developed on various platforms. 列表中输入的网站不一定使用Cookie,而是会在各种平台上开发。

Here's my login code: 这是我的登录代码:

r = requests.post(action, data=values, verify=False)
print(r.headers)
print(r.status_code)
print(r.content)
print(r.url)
sleep(1)

I'd like everything after that first line to be in an if statement if the login actually works, but I can't figure out how to actually determine that. 如果登录确实有效,我希望第一行之后的所有内容都在if语句中,但我不知道如何确定。

Thanks. 谢谢。

status bit will let you know 状态位会让您知道

 r = requests.post(action, data=values, verify=False)
 authi = requests.get(r.url, auth=('user', 'pass'))

>>> authi.status_code
200

You will find more status bit info here 您将在此处找到更多状态位信息

STATUS CODE DEFINITION 状态码定义

You can parse the r.content to check if you login or not, like string success or some flag means that you login. 您可以解析r.content来检查是否登录,例如字符串success或某些标志表示您登录。 if the r.content for login user and not login user are same, you can request a special link that need to login and find a flag to check. 如果登录用户和非登录用户的r.content相同,则可以请求需要登录的特殊链接并查找要检查的标志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM