2015-04-07 2 views
0

Я использую scrapy для проекта с конвейером, специально разработанным для полей элементов, которые необходимо вставить в базу данных. Я использую метод декоратора python для этого. По какой-то причине я не могу заставить свою голову обернуться вокруг этой проблемы, я получаю определенное имяError, которое я не знаю, откуда они взялись. Примечание: этот метод, который люди подтвердили, работает отлично.scrapy с конвейером каждого предмета, завернутым в файл паук

Это код, который я имею в моем 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 

и это ошибка acutal Я получение:

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

Любая идея, где это неправильно?

ответ

0

check_spider_pipeline "не определен", так как его не найдено. Он не находится в видимой области вашего сценария spider.py, но определен в локалях BoroughscrperPipeline.process_item. Вы должны сделать его видимым в своем объеме. Проверьте этот ответ, например: How can I use different pipelines for different spiders in a single Scrapy project

Смежные вопросы