要使页面只加载源代码而不加载其他文件,可以使用selenium的ChromeOptions类中的add_argument()方法来设置禁用自动加载图片和禁用JavaScript。以下是示例代码:

from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--disable-extensions')
chrome_options.add_argument('--disable-infobars')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-plugins-discovery')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-images')
chrome_options.add_argument('--disable-javascript')

driver = webdriver.Chrome(chrome_options=chrome_options)

在上面的代码中,我们添加了以下选项:

  • '--disable-images':禁用自动加载图片。
  • '--disable-javascript':禁用JavaScript。

这样页面只会加载源代码而不会加载其他文件

python 使用selenium时如何使页面只加载源代码不加载其他的文件

原文地址: https://www.cveoy.top/t/topic/fJjI 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录