我有一个很好的脚本在这里,这是为了打印网页的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)
1条答案
按热度按时间egdjgwm81#
选项一:绝对路径
您可以使用绝对路径或相对于
main.py
的路径:选项二:系统缓存目录
您也可以使用特定于平台的缓存目录和
use_cache_dir
选项:文档
这里有一些关于处理缓存文件的更多细节:https://requests-cache.readthedocs.io/en/stable/user_guide/files.html