Densetsu_No
Densetsu_No

Reputation: 63

Python requests returns 200 instead of 301

url = "https://www.avito.ma/fr/2_mars/sacs_et_accessoires/Ch%C3%A2les_en_Vrai_Soie_Chanel_avec_boite_38445885.htm"

try   
    r = requests.get(url,headers={'User-Agent': ua.random},timeout=timeout) # execute a timed website request
    if r.status_code > 299: # check for bad status
        r.raise_for_status() # if confirmed raise bad status
    else:
        print(r.status_code, url) # otherwise print status code and url
except Exception as e:
     print('\nThe following exception: {0}, \nhas been found found on the following post: "{1}".\n'.format(e,url))

Expected status = 301 Moved Permanently

You can visit the page or check http://www.redirect-checker.org/index.php with the url for a correct terminal print.

Returned status = 200 OK

The page has been moved and it should return the above 301 Moved Permanently, however it returns a 200. I read the requests doc and checked all the parameters (allow_redirects=False etc.) but I don't think it is a mistake of configuration.

I am puzzled at why requests wouldn't see the redirects.

Any ideas?

Thank you in advance.

Upvotes: 2

Views: 2071

Answers (1)

Jonathan Sánchez
Jonathan Sánchez

Reputation: 464

Python Requests module has the allow_redirect parameter in True by default. I've tested it with False and it gives the 301 code that you're looking for.

enter image description here

Note after reading your comment above: r.history saves each response_code before the one that you're right now which is saved in r.status_code (only if you leave the parameter in True).

Upvotes: 4

Related Questions