更改 python 请求中的引用 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20837786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:19:46  来源:igfitidea点击:

Changing the referer URL in python requests

pythonpython-2.7python-requestsreferer

提问by Mayank Kumar

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.

如果我使用请求库向网页发出 GET 请求,我该如何更改引用者。我翻遍了整个手册,但找不到。

采纳答案by simon

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

根据http://docs.python-requests.org/en/latest/user/advanced/#session-objects,您应该能够:

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

Or just:

要不就:

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

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

您的headersdict 将与默认/会话标头合并。从文档

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.

您传递给请求方法的任何字典都将与设置的会话级值合并。方法级参数覆盖会话参数。