revanchist
revanchist

Reputation: 63

Python Requests Refresh

I'm trying to use python's requests library to log in to a website. It's a pretty simple code, and you can really get the gist of requests just by going on its website. I, however, want to check if I'm successfully logged in via the url. The problem I've encountered is when I initiate the post requests and give it (the variable p) a url, whether the html has changed or not I'm still passed the same url when I type print(p.url). Is there any way for me to refresh the browser or update the url to whatever it's currently set at?

(I can add a line for checking the url against itself later, but for now I just want to get the correct url)

#!usr/bin/env python3


import requests

payload = {'login': 'USERNAME,
           'password': 'PASSWORD'}

with requests.Session() as s:
    p = s.post('WEBSITE', data=payload)
    #print p.text
    print(p.url)

Upvotes: 4

Views: 11700

Answers (1)

Feishi
Feishi

Reputation: 669

The usuage of python-requests may not as complex as you think. It will automatically handle the redirect of your post ( or session.get()).

Here, session.post() method return a response object:

r = s.post('website', data=payload)

which means r.url is current url you are looking for.

If you still want to refresh current page, just use:

s.get(r.url)

To verify whether you has login successfully, one solution is to do the login in your browser.

Based on the title or content of the webpage returned (i.e, use the content in r.text), you can judge whether you have made it.

BTW, python-requests is a great library, enjoy it.

Upvotes: 4

Related Questions