Python Selenium为中未使用的爬行器运行Firefox驱动程序

Python Selenium为中未使用的爬行器运行Firefox驱动程序,python,selenium,scrapy,Python,Selenium,Scrapy,我使用的Firefox驱动程序在我的项目中的一些爬行器中加载和废弃网页 问题: Selenium在运行所有爬行器时运行Firefox实例,这些爬行器是我没有导入的webdriver,也没有在中调用webdriver.Firefox() 预期行为: Selenium仅在运行中使用过的爬行器时运行Firfox实例 为什么这很重要? 我会在爬行器完成后退出Firefox实例,但很明显,这不会发生在不使用Selenium的爬行器中 未使用硒的蜘蛛 这个蜘蛛没有使用Selenium,我希望它不会运行Fir

我使用的Firefox驱动程序在我的项目中的一些爬行器中加载和废弃网页

问题:
Selenium在运行所有爬行器时运行Firefox实例,这些爬行器是我没有导入的
webdriver
,也没有在中调用
webdriver.Firefox()

预期行为:
Selenium仅在运行中使用过的爬行器时运行Firfox实例

为什么这很重要?
我会在爬行器完成后退出Firefox实例,但很明显,这不会发生在不使用Selenium的爬行器中

未使用硒的蜘蛛
这个蜘蛛没有使用Selenium,我希望它不会运行Firefox

class MySpider(scrapy.Spider):
    name = "MySpider"
    domain = 'www.example.com'
    allowed_domains = ['http://example.com']
    start_urls = ['http://example.com']

    def parse(self, response):
        for sel in response.css('.main-content'):
            # Article is a scrapy.item
            item = Article()
            item['title'] = sel.css('h1::text').extract()[0]
            item['body'] = sel.css('p::text').extract()[0]
            yield item

问题实际上在于我如何在打算使用Selenium的Spider中实例化
webdriver.Firefox
模块:

class MySpider(scrapy.Spider):
    # basic scrapy setting
    driver = webdriver.Firefox()

    def parse(self, response):
        self.driver.get(response.url)
        result = scrapy.Selector(text=self.driver.page_source)
        # scrap and yield items to pipeline
        # then in certain condition:
        self.driver.quit()
为什么会这样?
当运行Scrapy命令时,python解释项目中的所有类。因此,无论我尝试运行哪个spider,Selenium都会为包含此命令行的每个spider类运行一个新的
webdriver.Firefox

解决方案
刚刚将webdriver实例化移动到类init方法:

def __init__(self):
    self.driver = webdriver.Firefox()

请澄清您的具体问题或添加其他详细信息,以突出显示您所需的内容。正如目前所写的,很难准确地说出你在问什么。请参阅本页以获得澄清此问题的帮助。@JeffC希望现在清楚了。谢谢你,这没用。您的代码显示您正在
MySpider
类中实例化Firefox浏览器。为什么你认为它不会运行?@JeffC我试图解释一下,我只是在一些丢弃动态页面的爬行器中使用了Selenium,其余的爬行器不导入,因此根本不使用Selenium。但是当我运行它们的时候,Firefox的一个实例被打开,然后在你没有使用Firefox的地方发布代码,这样我们就可以看到不工作的代码。向我们展示工作代码并不能真正帮助我们解决问题。