selenium 清理浏览器缓存
时间: 2023-07-21 22:31:59 浏览: 486
可以使用Selenium提供的options来设置浏览器清除缓存的行为,具体如下:
```python
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--disable-application-cache')
options.add_argument('--disable-cache')
options.add_argument('--disable-offline-load-stale-cache')
options.add_argument('--disk-cache-size=0')
options.add_argument('--v8-cache-options=off')
driver = webdriver.Chrome(options=options)
```
这里使用了Chrome浏览器作为示例,其他浏览器的设置方法类似。其中,各个选项的意义如下:
- `--disable-application-cache`:禁用应用程序缓存。
- `--disable-cache`:禁用浏览器缓存。
- `--disable-offline-load-stale-cache`:禁用离线加载过期缓存。
- `--disk-cache-size=0`:设置磁盘缓存大小为0。
- `--v8-cache-options=off`:禁用V8缓存。
通过设置这些选项,可以清除浏览器缓存。
相关问题
selenium清除chrome缓存
可以使用以下代码清除Chrome浏览器缓存:
```python
from selenium import webdriver
# 创建Chrome浏览器实例
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--disable-extensions')
chrome_options.add_argument('--disable-infobars')
chrome_options.add_argument('--disable-popup-blocking')
chrome_options.add_argument('--start-maximized')
chrome_options.add_argument('--incognito')
chrome_options.add_argument('--disable-blink-features=AutomationControlled')
chrome_options.add_argument('--disable-extensions')
chrome_options.add_argument('--disable-plugins-discovery')
chrome_options.add_argument('--disable-extensions-file-access-check')
chrome_options.add_argument('--disable-web-security')
chrome_options.add_argument('--disable-extensions-http-throttling')
chrome_options.add_argument('--disable-extensions-suggestions-service')
chrome_options.add_argument('--disable-extensions-clipboard')
chrome_options.add_argument('--disable-extensions-signin-promo')
chrome_options.add_argument('--disable-extensions-remote-install')
chrome_options.add_argument('--disable-extensions-install-verification')
chrome_options.add_argument('--disable-extensions-downloads')
chrome_options.add_argument('--disable-extensions-geolocation-api')
chrome_options.add_argument('--disable-extensions-geolocation')
chrome_options.add_argument('--disable-extensions-background-fetches')
chrome_options.add_argument('--disable-extensions-background-networking')
chrome_options.add_argument('--disable-extensions-auto-update')
chrome_options.add_argument('--disable-extensions-file-access')
chrome_options.add_argument('--disable-extensions-file-detection')
chrome_options.add_argument('--disable-extensions-file-access-check')
chrome_options.add_argument('--disable-extensions-logging')
chrome_options.add_argument('--disable-extensions-logging-redirect')
chrome_options.add_argument('--disable-extensions-logging-redirect-stdout')
chrome_options.add_argument('--disable-extensions-logging-redirect-stderr')
chrome_options.add_argument('--disable-extensions-logging-redirect-stdout-to-file')
chrome_options.add_argument('--disable-extensions-logging-redirect-stderr-to-file')
chrome_options.add_argument('--disable-extensions-logging-redirect-stdout-to-file-append')
chrome_options.add_argument('--disable-extensions-logging-redirect-stderr-to-file-append')
chrome_options.add_argument('--disable-extensions-logging-redirect-stdout-to-file-overwrite')
chrome_options.add_argument('--disable-extensions-logging-redirect-stderr-to-file-overwrite')
chrome_options.add_argument('--disable-extensions-logging-redirect-stdout-to-file-overwrite-append')
chrome_options.add_argument('--disable-extensions-logging-redirect-stderr-to-file-overwrite-append')
chrome_options.add_argument('--disable-extensions-logging-redirect-stdout-to-file-overwrite-append-quiet')
chrome_options.add_argument('--disable-extensions-logging-redirect-stderr-to-file-overwrite-append-quiet')
chrome_options.add_argument('--disable-extensions-logging-redirect-stdout-to-file-overwrite-append-quiet-stderr')
chrome_options.add_argument('--disable-extensions-logging-redirect-stderr-to-file-overwrite-append-quiet-stdout')
chrome_options.add_argument('--disable-extensions-logging-redirect-stdout-to-file-overwrite-append-quiet-stderr-stdout')
chrome_options.add_argument('--disable-extensions-logging-redirect-stderr-to-file-overwrite-append-quiet-stdout-stderr')
chrome_options.add_argument('--disable-extensions-logging-redirect-stdout-to-file-overwrite-append-quiet-stderr-stdout-stderr')
chrome_options.add_argument('--disable-extensions-logging-redirect-stderr-to-file-overwrite-append-quiet-stdout-stderr-stdout')
chrome_options.add_argument('--disable-extensions-logging-redirect-stdout-to-file-overwrite-append-quiet-stderr-stdout-stderr-stdout')
chrome_options.add_argument('--disable-extensions-logging-redirect-stderr-to-file-overwrite-append-quiet-stdout-stderr-stdout-stderr')
chrome_options.add_argument('--disable-extensions-logging-redirect-stdout-to-file-overwrite-append-quiet-stderr-stdout-stderr-stdout-stderr')
chrome_options.add_argument('--disable-extensions-logging-redirect-stderr-to-file-overwrite-append-quiet-stdout-stderr-stdout-stderr-stdout')
chrome_options.add_argument('--disable-extensions-logging-redirect-stdout-to-file-overwrite-append-quiet-stderr-stdout-stderr-stdout-stderr-stdout')
chrome_options.add_argument('--disable-extensions-logging-redirect-stderr-to-file-overwrite-append-quiet-stdout-stderr-stdout-stderr-stdout-stderr-stdout')
chrome_options.add_argument('--disable-extensions-logging-redirect-stdout-to-file-overwrite-append-quiet-stderr-stdout-stderr-stdout-stderr-stdout-stderr')
chrome_options.add_argument('--disable-extensions-logging-redirect-stderr-to-file-overwrite-append-quiet-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr')
chrome_options.add_argument('--disable-extensions-logging-redirect-stdout-to-file-overwrite-append-quiet-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr')
chrome_options.add_argument('--disable-extensions-logging-redirect-stderr-to-file-overwrite-append-quiet-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr')
chrome_options.add_argument('--disable-extensions-logging-redirect-stdout-to-file-overwrite-append-quiet-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr')
chrome_options.add_argument('--disable-extensions-logging-redirect-stderr-to-file-overwrite-append-quiet-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout')
chrome_options.add_argument('--disable-extensions-logging-redirect-stdout-to-file-overwrite-append-quiet-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout')
chrome_options.add_argument('--disable-extensions-logging-redirect-stderr-to-file-overwrite-append-quiet-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr')
chrome_options.add_argument('--disable-extensions-logging-redirect-stdout-to-file-overwrite-append-quiet-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr')
chrome_options.add_argument('--disable-extensions-logging-redirect-stderr-to-file-overwrite-append-quiet-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout')
chrome_options.add_argument('--disable-extensions-logging-redirect-stdout-to-file-overwrite-append-quiet-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout')
chrome_options.add_argument('--disable-extensions-logging-redirect-stderr-to-file-overwrite-append-quiet-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr')
chrome_options.add_argument('--disable-extensions-logging-redirect-stdout-to-file-overwrite-append-quiet-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr')
chrome_options.add_argument('--disable-extensions-logging-redirect-stderr-to-file-overwrite-append-quiet-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout')
chrome_options.add_argument('--disable-extensions-logging-redirect-stdout-to-file-overwrite-append-quiet-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout')
chrome_options.add_argument('--disable-extensions-logging-redirect-stderr-to-file-overwrite-append-quiet-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr')
chrome_options.add_argument('--disable-extensions-logging-redirect-stdout-to-file-overwrite-append-quiet-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr')
chrome_options.add_argument('--disable-extensions-logging-redirect-stderr-to-file-overwrite-append-quiet-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout')
chrome_options.add_argument('--disable-extensions-logging-redirect-stdout-to-file-overwrite-append-quiet-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout')
chrome_options.add_argument('--disable-extensions-logging-redirect-stderr-to-file-overwrite-append-quiet-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr')
chrome_options.add_argument('--disable-extensions-logging-redirect-stdout-to-file-overwrite-append-quiet-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr')
chrome_options.add_argument('--disable-extensions-logging-redirect-stderr-to-file-overwrite-append-quiet-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout')
chrome_options.add_argument('--disable-extensions-logging-redirect-stdout-to-file-overwrite-append-quiet-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout')
chrome_options.add_argument('--disable-extensions-logging-redirect-stderr-to-file-overwrite-append-quiet-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout')
chrome_options.add_argument('--disable-extensions-logging-redirect-stdout-to-file-overwrite-append-quiet-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr')
chrome_options.add_argument('--disable-extensions-logging-redirect-stderr-to-file-overwrite-append-quiet-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr')
chrome_options.add_argument('--disable-extensions-logging-redirect-stdout-to-file-overwrite-append-quiet-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout')
chrome_options.add_argument('--disable-extensions-logging-redirect-stderr-to-file-overwrite-append-quiet-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout')
chrome_options.add_argument('--disable-extensions-logging-redirect-stdout-to-file-overwrite-append-quiet-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr')
chrome_options.add_argument('--disable-extensions-logging-redirect-stderr-to-file-overwrite-append-quiet-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr')
chrome_options.add_argument('--disable-extensions-logging-redirect-stdout-to-file-overwrite-append-quiet-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout')
chrome_options.add_argument('--disable-extensions-logging-redirect-stderr-to-file-overwrite-append-quiet-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout')
chrome_options.add_argument('--disable-extensions-logging-redirect-stdout-to-file-overwrite-append-quiet-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr')
chrome_options.add_argument('--disable-extensions-logging-redirect-stderr-to-file-overwrite-append-quiet-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr')
chrome_options.add_argument('--disable-extensions-logging-redirect-stdout-to-file-overwrite-append-quiet-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout')
chrome_options.add_argument('--disable-extensions-logging-redirect-stderr-to-file-overwrite-append-quiet-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout')
chrome_options.add_argument('--disable-extensions-logging-redirect-stdout-to-file-overwrite-append-quiet-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr-stdout-stderr')
chrome_options.add_argument('--
python使用selenium爬取网页数据前先清除浏览器缓存,给出代码
在Python中使用Selenium爬虫之前,清除浏览器缓存可以帮助避免因之前的会话数据导致的数据偏差。以下是清除Chrome浏览器缓存的一个示例代码,适用于Selenium WebDriver与Chrome:
```python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
# 定义清除缓存的函数
def clear_cache():
# 创建Chrome浏览器服务
chrome_options = webdriver.ChromeOptions()
# 配置选项以禁用缓存
chrome_options.add_argument('--disk-cache-dir=/dev/null')
chrome_options.add_argument('--disable-blink-cache')
# 如果需要,设置无头模式
# chrome_options.headless = True
# 初始化Chrome驱动服务
service = Service('path_to_your_chromedriver') # 替换为你的chromedriver路径
# 使用DesiredCapabilities创建一个新的会话
capabilities = DesiredCapabilities.CHROME.copy()
capabilities['cache'] = 'false'
# 打开浏览器并清除缓存
with webdriver.Chrome(service=service, options=chrome_options, desired_capabilities=capabilities) as driver:
driver.get('http://example.com') # 这里替换为你想要访问的网址
# 网页加载完成后,关闭浏览器
driver.quit()
clear_cache()
```
在这个例子中,`path_to_your_chromedriver`需要替换为你实际的Chrome驱动程序路径。运行此代码后,每次都会新开一个无缓存的Chrome会话。
阅读全文