简体   繁体   English

Python 请求缓存仍在查询远程 URL

[英]Python Requests-Cache still querying remote URL

I'm using the Requests-Cache library to cache results from Requests .我正在使用Requests-Cache库来缓存Requests的结果。 It appears to install a cache just fine;似乎可以很好地安装缓存; requesting a URL creates a .sqlite cache file, and subsequent requests retrieve that data, even if the remote page changes.请求 URL 会创建一个.sqlite缓存文件,即使远程页面发生更改,后续请求也会检索该数据。

My internet connection is rather poor today, and I noticed my script (which makes many (supposedly cached) requests) was running slowly.今天我的互联网连接很差,我注意到我的脚本(它发出许多(据说是缓存的)请求)运行缓慢。 As a quick sanity check, I tried a test script to make a cache, then ran it again after disconnecting my computer from wifi.作为一个快速的健全性检查,我尝试了一个测试脚本来制作缓存,然后在将我的计算机与 wifi 断开连接后再次运行它。 However, this errors out:但是,这会出错:

requests.exceptions.ConnectionError: HTTPConnectionPool(host='magicplugin.normalitycomics.com', port=80): Max retries exceeded with url: /update/updatelist.txt (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x110390d68>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known'))

Why is the request even trying to connect to the remote site, if Requests-Cache is redirecting it to use the local cached data?为什么请求甚至尝试连接到远程站点,如果 Requests-Cache 正在重定向它以使用本地缓存数据? Is there a way to avoid this?有没有办法避免这种情况? I don't need to slow down my script (particularly if my connection is poor) and make unnecessary requests from the server.我不需要减慢我的脚本(特别是如果我的连接很差)并从服务器发出不必要的请求。

I figured it out!我想到了!

My actual code makes requests that sometimes successfully get pages, and sometimes get a 404.我的实际代码发出的请求有时会成功获取页面,有时会得到 404。

The only reason my simple test script replicated the problem was that I made a typo in the page I was requesting.我的简单测试脚本复制问题的唯一原因是我在请求的页面中输入了错字。 Requests received a 404. Even though Requests-Cache created a cache file, it did not store this result in it. Requests 收到 404。即使 Requests-Cache 创建了一个缓存文件,它也没有将这个结果存储在其中。

It turns out that by default, Requests-Cache only caches 200-code responses, but this is configurable :事实证明,默认情况下,Requests-Cache 仅缓存 200 个代码响应,但这是可配置的

requests_cache.install_cache('example_cache', allowable_codes=(200, 404))

And now it works fine!现在它工作正常!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM