scrapy 废 selenium :Chrome驱动程序无法加载页面

voase2hg  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(190)

我有两个项目,一个使用Selenium,另一个使用Scrapy-Selenium,Scrapy-Selenium适合Scrapy spider程序格式,但使用Selenium实现自动化。
我可以让Chromedriver为Selenium基本程序加载我想要的页面,但是第二个项目(Scrapy)的一些东西阻止了它加载URL,而是在URL栏中显示data:,
第一个项目(工程细):

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(executable_path="./chromedriver")
driver.get("https://ricemedia.co")

第二个项目(不加载页面):

import scrapy
from scrapy_selenium import SeleniumRequest
from selenium import webdriver
import time

class ExampleSpider(scrapy.Spider):
    name = 'rice'

    def start_requests(self):
        yield SeleniumRequest(
            url="https://ricemedia.co",
            wait_time=3,
            callback=self.parse
        )

    def parse(self, response):
        driver = webdriver.Chrome(executable_path="./chromedriver")
        driver.maximize_window()
        time.sleep(20)

我浏览过StackOverflow和Google,两个最常见的原因是Chrome驱动程序过时和URL中缺少http。我的情况都不是这样。chromedriver的路径看起来也不错(这两个项目在同一个文件夹中,沿着同一个chromedriver)。既然一个能用,另一个不能用,这应该和我的Scrapy-Selenium蜘蛛有关。
我应该补充的是,我已经在我的虚拟环境中使用pip本地安装了Scrapy、Selenium和Scrapy-Selenium,我怀疑这是安装问题。
请帮忙,谢谢!

mwkjh3gx

mwkjh3gx1#

您可以使用另一种方法来安装chrome驱动程序:首先,使用以下pip install webdriver-manager安装Webdriver管理器,或者使用maven dep获取它
然后代码:


# selenium 3

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())

相关问题