0

我正在抓取一个 javascript 繁重的网站,我已经设置了一个 vagrant 实例来检查可行性(1GB RAM)。解析几个 url 后系统崩溃。我无法确定此设置的内存要求和崩溃原因。但是,我让 htop 并行运行,并在系统崩溃之前获得了屏幕截图,附在下面。我怀疑内存不够,但我不知道我需要多少。因此,我正在寻找:

  1. 我的设置的内存要求( Scrapy + selenium + fireofox -headless
  2. 坠机原因
  3. 如何改进刮削工艺
  4. (scrapy, selenium, firefox) 的替代品

SeleniumMiddleWare:

import os, traceback
from shutilwhich import which
from scrapy import signals
from scrapy.http import HtmlResponse
from scrapy.utils.project import get_project_settings
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.firefox.options import Options

SELENIUM_HEADLESS = False

settings = get_project_settings()

class SeleniumMiddleware(object):
    driver = None

    @classmethod
    def from_crawler(cls, crawler):
        middleware = cls()
        crawler.signals.connect(middleware.spider_opened, signals.spider_opened)
        crawler.signals.connect(middleware.spider_closed, signals.spider_closed)
        return middleware

    def process_request(self, request, spider):
        if not request.meta.get('selenium'):
            return
        self.driver.get(request.url)

        #if setting new cookies remove old
        if len(request.cookies):
            self.driver.implicitly_wait(1)
            self.driver.delete_all_cookies()

        #add only desired cookies if session persistence is requested
        request_cookies=[]

        if request.meta.get('request_cookies'):
           request_cookies=request.meta.get('request_cookies')

        for cookie in request.cookies:
             if cookie['name'] in request_cookies:
                print ' ---- set request cookie [%s] ---- ' % cookie['name']
                new_cookie={k: cookie[k] for k in ('name', 'value','path', 'expiry') if k in cookie}
                self.driver.add_cookie(new_cookie)

        if  request.meta.get('redirect_url'):
            self.driver.get(request.meta.get('redirect_url'))
            self.driver.implicitly_wait(5)

        request.meta['driver'] = self.driver

        return HtmlResponse(self.driver.current_url, body=self.driver.page_source, encoding='utf-8', request=request)

    def spider_opened(self, spider):
        options=Options()
        binary= settings.get('SELENIUM_FIREFOX_BINARY') or which('firefox')
        SELENIUM_HEADLESS=settings.get('SELENIUM_HEADLESS') or False
        if SELENIUM_HEADLESS:
            print " ---- HEADLESS ----"
            options.add_argument( "--headless" )

        firefox_capabilities = DesiredCapabilities.FIREFOX
        firefox_capabilities['marionette'] = True
        firefox_capabilities['binary'] = binary
        try:
            self.driver = webdriver.Firefox(capabilities=firefox_capabilities, firefox_options=options)
        except Exception:
          print " ---- Unable to instantiate selenium webdriver instance ! ----"
          traceback.print_exc()
          os._exit(1)

    def spider_closed(self, spider):
        if self.driver:
            self.driver.close()

在此处输入图像描述

4

1 回答 1

0

快速潜入您的代码块显示def spider_closed(self, spider):您正在使用self.driver.close()如下:

def spider_closed(self, spider):
    if self.driver:
        self.driver.close()

根据关闭webdriver变体和webbrowser实例的最佳实践,您应该quit()tearDown() {}. 调用,通过发送命令quit()删除当前浏览会话,最后发送 GET 请求。quit{"flags":["eForceQuit"]}/shutdown EndPoint

您可以在不调用 driver.quit() 的情况下如何停止影响 PC 内存的 geckodriver 进程中找到详细讨论?

所以解决方案是替换self.driver.close()如下self.driver.quit()

def spider_closed(self, spider):
    if self.driver:
        self.driver.quit()
于 2018-01-25T14:43:49.947 回答