繁体   English   中英

抓斗,每个项目管道都包裹在蜘蛛文件中

[英]scrapy with each item pipeline wrapped within spider file

我将scrapy用于项目管道,该项目专为需要将项目字段插入数据库的项目管道而设计。 我正在使用python装饰器方法来使其工作。 由于某种原因,我无法解决这个问题,我遇到了特定的nameError,我不确定它们来自哪里。 注意:人们已经确认此方法工作正常。

这是我的spider.py文件中的代码:

from scrapy.spider import Spider
from scrapy.http import Request,FormRequest
from exampleScraper.items import exampleItem
import urllib, time, MySQLdb, sys

today = time.strftime("%x %X")

class idoxpaSpider(Spider):
  pipeline = set(['insert'])

  name = 'idoxpaSpider'

  start_urls = ["http://www.example.com"]
  ###
  def parse(self, response):
    # some scrapy work 
    return item

###
class insert(object):
  def __init__(self):
    self.conn = MySQLdb.connect(<some parameters>)
    self.cursor = self.conn.cursor()

  @check_spider_pipeline
  def process_item(self, item, spider):
    return item

这就是我在管道文件中的内容:

import sys

class BoroughscrperPipeline(object):
  def process_item(self, item, spider):
    def check_spider_pipeline(process_item_method):
      @functools.wraps(process_item_method)
      def wrapper(self, item, spider):
      # message template for debugging
        msg = '%%s %s pipeline step' % (self.__class__.__name__,)

        # if class is in the spider pipeline then use use process_item normally
        if self.__class__ in spider.pipeline:
          spider.log(msg % 'executing', level=log.DEBUG)
          return process_item_method(self, item, spider)

      # otherwise return the untouched item
        else:
          spider.log(msg % 'skipping', level=log.DEBUG)
          return item
      return wrapper

这是我得到的严重错误:

File "/home/mn/workbench/boroughScrper/boroughScrper/spiders/westminsterSpider.py", line 40, in insert
    @check_spider_pipeline
NameError: name 'check_spider_pipeline' is not defined

知道哪里出问题了吗?

check_spider_pipeline为“未定义”,因为找不到它。 它不在您的spider.py脚本可见的范围内,而是在BoroughscrperPipeline.process_item的本地语言中定义的。 您需要使其在您的范围内可见。 例如,请查看以下答案: 如何在一个Scrapy项目中为不同的Spider使用不同的管道

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM