scrapy __init__()获得意外的关键字参数“_job”

soat7uwm  于 2023-01-13  发布在  其他
关注(0)|答案(2)|浏览(230)

我试着把scravyd和scrappy连用。
当我使用下面的代码时,它工作正常。

import scrapy
from scrapy.selector import Selector
from scrapy.spiders import CrawlSpider
from shikari.items import shikariItem
from scrapy.http import Request

class Radiate(scrapy.Spider) :
  name = "run"

  allowed_domains = ["google.com"]
  start_urls = ['http://google.com']

  def parse(self, response) :
    item = shikariItem () 
    sel = Selector (response)
    item['url'] = response.url
    return item

但当我把它和 selenium 一起使用时,它就不会

import scrapy
from pyvirtualdisplay import Display
from selenium import webdriver
from shikari.items import shikariItem

class Radiate(scrapy.Spider) :
  name = "run"

  allowed_domains = ["google.com"]
  start_urls = ['http://google.com']

  def __init__(self):
   display = Display(visible=0, size=(800, 600))
   display.start()
   self.driver = webdriver.Chrome('/usr/bin/chromedriver')  

  def parse(self, response) :
    item = shikariItem () 
    item['url'] = self.driver.get(response.url)
    return item

  def teardown(self): 
    display.stop()
    self.driver.close()

我的蜘蛛从来没有运行过。在作业中,它被列在“已完成”下,在错误日志中,我看到exceptions.TypeError: __init__() got an unexpected keyword argument '_job'
下面是完整的错误日志

2016-02-23 19:42:06 [scrapy] INFO: Scrapy 1.0.5 started (bot: scrapybot)
2016-02-23 19:42:06 [scrapy] INFO: Optional features available: ssl, http11
2016-02-23 19:42:06 [scrapy] INFO: Overridden settings: {'NEWSPIDER_MODULE': 'shikari.spiders', 'FEED_URI': 'file:///var/www/html/shikari/scrapyd-client-master/items/shikari/run/82776010da6511e5b08d1288e0cebe8d.jl', 'CONCURRENT_REQUESTS_PER_DOMAIN': 1, 'SPIDER_MODULES': ['shikari.spiders'], 'USER_AGENT': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0', 'FEED_FORMAT': 'csv', 'LOG_FILE': 'logs/shikari/run/82776010da6511e5b08d1288e0cebe8d.log', 'DOWNLOAD_DELAY': 4}
2016-02-23 19:42:06 [scrapy] INFO: Enabled extensions: CloseSpider, FeedExporter, TelnetConsole, LogStats, CoreStats, SpiderState
2016-02-23 19:42:06 [twisted] ERROR: Unhandled error in Deferred:
2016-02-23 19:42:06 [twisted] ERROR: Unhandled Error
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/scrapy/cmdline.py", line 150, in _run_command
    cmd.run(args, opts)
  File "/usr/local/lib/python2.7/dist-packages/scrapy/commands/crawl.py", line 57, in run
    self.crawler_process.crawl(spname, **opts.spargs)
  File "/usr/local/lib/python2.7/dist-packages/scrapy/crawler.py", line 153, in crawl
    d = crawler.crawl(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/twisted/internet/defer.py", line 1237, in unwindGenerator
    return _inlineCallbacks(None, gen, Deferred())
--- <exception caught here> ---
  File "/usr/lib/python2.7/dist-packages/twisted/internet/defer.py", line 1099, in _inlineCallbacks
    result = g.send(result)
  File "/usr/local/lib/python2.7/dist-packages/scrapy/crawler.py", line 70, in crawl
    self.spider = self._create_spider(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/scrapy/crawler.py", line 80, in _create_spider
    return self.spidercls.from_crawler(self, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/scrapy/spiders/__init__.py", line 50, in from_crawler
    spider = cls(*args, **kwargs)
exceptions.TypeError: __init__() got an unexpected keyword argument '_job'

我需要做什么才不会收到错误?

yftpprvb

yftpprvb1#

您在子类中重新定义了__init__,并将其签名从scrapy.Spider更改为scrapy.Spider,也没有调用scrapy.Spider__init__方法。

    • 初始化**的scrapy.Spider源代码
zphenhs4

zphenhs42#

你的init方法中没有任何参数,只需添加args*和***kwargs***。

def __init__(*args, **kwargs):
        display = Display(visible=0, size=(800, 600))
        display.start()
        self.driver = webdriver.Chrome('/usr/bin/chromedriver')

相关问题