简体   繁体   中英

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.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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