Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the referer URL in python requests

How do I change the referer if I'm using the requests library to make a GET request to a web page. I went through the entire manual but couldn't find it.

like image 501
Mayank Kumar Avatar asked Dec 30 '13 10:12

Mayank Kumar


People also ask

How do I change my referral URL?

If you want to change the referer (url) header that will be sent to the server when a user clicks an anchor or iframe is opened, you can do it without any hacks. Simply do history. replaceState, you will change the url as it will appear in the browser bar and also the referer that will be send to the server.

What is URL in request in Python?

url returns the URL of the response. It will show the main url which has returned the content, after all redirections, if done. Python requests are generally used to fetch the content from a particular resource URI. Whenever we make a request to a specified URI through Python, it returns a response object.

How do I get referer from HTTP request?

It's available in the HTTP referer header. You can get it in a servlet as follows: String referrer = request. getHeader("referer"); // Yes, with the legendary misspelling.


1 Answers

According to http://docs.python-requests.org/en/latest/user/advanced/#session-objects , you should be able to do:

s = requests.Session() s.headers.update({'referer': my_referer}) s.get(url) 

Or just:

requests.get(url, headers={'referer': my_referer}) 

Your headers dict will be merged with the default/session headers. From the docs:

Any dictionaries that you pass to a request method will be merged with the session-level values that are set. The method-level parameters override session parameters.

like image 64
simon Avatar answered Sep 17 '22 16:09

simon