如何使用 Python 请求清除缓存?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20198274/
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
How do I clear cache with Python Requests?
提问by ethanjyx
Does the requestspackage of Python caches data by default?
requestsPython的包是否默认缓存数据?
For example,
例如,
import requests
resp = requests.get('https://some website')
Will the response be cached? If yes, how do I clear it?
响应会被缓存吗?如果是,我该如何清除它?
采纳答案by ethanjyx
Python-requests doesn't have any caching features.
Python-requests 没有任何缓存功能。
However, if you need them you can look at requests-cache, although I never used it.
但是,如果您需要它们,您可以查看requests-cache,尽管我从未使用过它。
As of 2018, its last release was in 2016, however, so it doesn't appear to be maintained.
截至 2018 年,它的最后一次发布是在 2016 年,因此它似乎没有得到维护。
回答by Lukasa
Requests does not do caching by default. You can easily plug it in by using something like CacheControl.
默认情况下,请求不做缓存。您可以使用CacheControl 之类的东西轻松插入它。
回答by jones77
Add a 'Cache-Control: no-cache'header:
添加'Cache-Control: no-cache'标题:
self.request = requests.get('http://google.com',
headers={'Cache-Control': 'no-cache'})
See https://stackoverflow.com/a/55613686/469045for complete answer.
有关完整答案,请参阅https://stackoverflow.com/a/55613686/469045。
回答by CONvid19
Late answer, but python requestsdoesn't cache requests,you should use the headersCache-Controland Pragmainstead, i.e.:
迟到的答案,但python 请求不缓存请求,你应该使用标头Cache-Control,Pragma而不是,即:
import requests
headers = {
...
"Cache-Control": "no-cache",
"Pragma": "no-cache"
}
x = requests.get("site.tld", headers=headers)
...
Cache-Control
The Cache-Control general-header field is used to specify directives for caching mechanisms in both requests and responses. Caching directives are unidirectional, meaning that a given directive in a request is not implying that the same directive is to be given in the response.Pragma
Implementation-specific header that may have various effects anywhere along the request-response chain. Used for backwards compatibility with HTTP/1.0 caches where the Cache-Control header is not yet present.
Cache-Control
Cache-Control 通用标头字段用于为请求和响应中的缓存机制指定指令。缓存指令是单向的,这意味着请求中的给定指令并不意味着响应中将给出相同的指令。Pragma
特定于实现的标头可能在请求-响应链的任何地方产生各种影响。用于向后兼容 HTTP/1.0 缓存,其中 Cache-Control 标头尚不存在。
Directive
指示
no-cache
Forces caches to submit the request to the origin server for validation before releasing a cached copy.
no-cache
强制缓存在释放缓存副本之前将请求提交给源服务器进行验证。
Note on Pragma:
注意Pragma:
Pragma is not specified for HTTP responses and is therefore not a reliable replacement for the general HTTP/1.1 Cache-Control header, although it does behave the same as Cache-Control: no-cache, if the Cache-Control header field is omitted in a request. Use Pragma only for backwards compatibility with HTTP/1.0 clients.
没有为 HTTP 响应指定 Pragma,因此它不是通用 HTTP/1.1 Cache-Control 标头的可靠替代品,尽管它的行为与 Cache-Control: no-cache 相同,如果 Cache-Control 标头字段在一个要求。使用 Pragma 仅用于向后兼容 HTTP/1.0 客户端。

