Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When I use python requests to check a site, if the site redirects me to another page, will I know?

What I mean is, if I go to "www.yahoo.com/thispage", and yahoo has set up a filter to redirect /thispage to /thatpage. So whenever someone goes to /thispage, s/he will land on /thatpage.

If I use httplib/requests/urllib, will it know that there was a redirection? What error pages? Some sites redirect user to /errorpage whenever the page cannot be found.

like image 891
iCodeLikeImDrunk Avatar asked Nov 20 '12 21:11

iCodeLikeImDrunk


People also ask

What does it mean when a website redirects you?

On a Web site, redirection is a technique for moving visitors to a different Web page than the one they request, usually because the page requested is unavailable. Web users often encounter redirection when they visit the Web site of a company whose name has been changed or which has been acquired by another company.


2 Answers

With requests, you get a listing of any redirects in the .history attribute of the response object. It returns a Python list. See the documentation for more.

like image 127
MikeHunter Avatar answered Sep 25 '22 19:09

MikeHunter


To prevent requests from following redirects use:

r = requests.get('http://www.yahoo.com/thispage', allow_redirects=False)

If it is in indeed a redirect, you can check the redirect target location in r.headers['location'].

like image 38
yonilevy Avatar answered Sep 22 '22 19:09

yonilevy