我正在抓取一个 javascript 繁重的网站,我已经设置了一个 vagrant 实例来检查可行性(1GB RAM)。解析几个 url 后系统崩溃。我无法确定此设置的内存要求和崩溃原因。但是,我让 htop 并行运行,并在系统崩溃之前获得了屏幕截图,附在下面。我怀疑内存不够,但我不知道我需要多少。因此,我正在寻找:
- 我的设置的内存要求( Scrapy + selenium + fireofox -headless
- 坠机原因
- 如何改进刮削工艺
- (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()