Created
September 18, 2020 07:43
-
-
Save howardjones/d3ba9327017d013c1e31ddf1fcfa3ef6 to your computer and use it in GitHub Desktop.
So you have a crawler, but you don't want it to cache images, just the page.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from cachecontrol import CacheControl | |
from cachecontrol.caches import FileCache | |
from cachecontrol.heuristics import BaseHeuristic | |
class NoImagesHeuristic(BaseHeuristic): | |
def update_headers(self, response): | |
if response.headers['Content-Type'].startswith("image/"): | |
return { | |
'cache-control': 'no-cache', | |
} | |
return {} | |
sess = requests.session() | |
cached_sess = CacheControl(requests.Session(), | |
heuristic=NoImagesHeuristic(), | |
cache=FileCache('.webcache') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment