如何在不同的目录中构建`requests_cache` cache.sqlite

goucqfw6  于 2023-06-23  发布在  SQLite
关注(0)|答案(1)|浏览(124)

我有一个很好的脚本在这里,这是为了打印网页的HTML。如果我像python3 main.py一样执行它,它会在当前目录中创建一个webpage_cache.sqlite。如果通过exec(open("main.py").read())在interactive中运行它,它不会在当前目录中创建它。有没有什么方法可以让这个脚本运行,并把它放在一个自定义位置,这样我就不会得到很多额外的文件,很少缓存,因为它不会找到我的缓存?脚本如下:

import requests
import requests_cache

requests_cache.install_cache('webpage_cache')

# URL of the webpage to fetch
url = input("What URL should I fetch?")

# Make a GET request to the webpage
response = requests.get(url)

# Check if the response was retrieved from the cache
if response.from_cache:
    print('Retrieved from cache')
else:
    print('Fetched from website')

# Print the HTML content of the webpage
print(response.text)
egdjgwm8

egdjgwm81#

选项一:绝对路径

您可以使用绝对路径或相对于main.py的路径:

from pathlib import Path
from requests_cache import CachedSession

# Absolute
cache_path = Path('~/webpage_cache.sqlite').expanduser()

# Relative to current file
cache_path = Path(__file__) / 'webpage_cache.sqlite'

session = CachedSession(cache_path)
print(f'Using cache location: {session.cache.db_path}')

url = input('What URL should I fetch?')
response = session.get(url)

print(f'Retrieved from cache: {response.from_cache}')
print(response.text)

选项二:系统缓存目录

您也可以使用特定于平台的缓存目录和use_cache_dir选项:

from requests_cache import CachedSession

session = CachedSession('webpage_cache', use_cache_dir=True)
print(f'Using cache location: {session.cache.db_path}')
...

文档

这里有一些关于处理缓存文件的更多细节:https://requests-cache.readthedocs.io/en/stable/user_guide/files.html

相关问题