首页 > 解决方案 > 页面加载后如何使硒刮页面

问题描述

我正在使用 scrapy 来抓取所有链接,并使用 selenium 来抓取所有页面。Selenium 刮掉了大部分页面,但由于页面加载需要时间而留下了一些页面。

我尝试了 timeout() ,但似乎没有用,然后我尝试了 execute_script

driver.execute_script("return document.readyState=="complete";")

这似乎也不起作用,然后我尝试了 expected_conditions

WebDriverWait.until(expected_conditions.execute_script("return document.readyState=="complete";"))

但似乎没有用

我正在使用 firefox 浏览器,用于 Headless 的 phantomJs 尝试使用 Chrome 驱动程序安装,brew cask install chromedriver但我遇到了这个错误

raise WebDriverException("Can not connect to the Service %s" % self.path) selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service chromedriver

所以回到phantomjs。

谢谢!

标签: pythonseleniumweb-scrapingscrapymacos-catalina

解决方案


raise WebDriverException("Can not connect to the Service %s" % self.path) selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service chromedriver

这是因为您的程序无法通过给定的 chromedriver.exe 连接到服务,这可能是由于版本不匹配或可执行文件不可用而发生的。

您可以按如下方式解决它:

  • 检查您在系统上使用的 chrome 浏览器的版本,您可以在 chrome 设置 > 关于 chrome 中查看。然后在此处相应地下载 chromedriver: https ://chromedriver.chromium.org/downloads

  • 您可以将其存储在任何地方,但最好将其保存在与代码相同的目录中。将其解压缩并将其复制到相应的目录中,您就可以使用 chromedriver。

  • 如果它与您的程序不在同一目录中,请取消注释driver = webdriver.Chrome()或使用。driver = webdriver.Chrome(executable_path=r'your path here')


推荐阅读