2012-05-04 4 views
0

내가 scrapy에 대한 MySQL의 파이프 라인을 쓰기 위해 노력하고있어,하지만 난에 "가져 오기 오류"를 얻을 :없음 모듈 이름 예외 - scrapy 파이프 라인

File "C:\Python27\lib\site-packages\scrapy-0.14.3-py2.7-win32.egg\scrapy\utils\misc.py", line 39,  
in load_object raise ImportError, "Error loading object '%s': %s" % (path, e) 
ImportError: Error loading object 'tutorial.pipelines.MySQLStore': No module named exceptions 
파이프 라인의 코드는

:

from scrapy import log 
from scrapy.core.exceptions import DropItem 
from twisted.enterprise import adbapi 

import time 
import MySQLdb.cursors 

class FilterWords(object): 
    """A pipeline for filtering out items which contain certain words in their 
    description""" 

    # put all words in lowercase 
    words_to_filter = ['politics', 'religion'] 

     def process_item(self, spider, item): 
     print spider 
     for word in self.words_to_filter: 
      if word in unicode(item['description']).lower(): 
       raise DropItem("Contains forbidden word: %s" % word) 
     else: 
      return item 

class MySQLStore(object): 

    def __init__(self): 
     # @@@ hardcoded db settings 
     # TODO: make settings configurable through settings 
     self.dbpool = adbapi.ConnectionPool('', 
      db='', 
      user='', 
      passwd='', 
      cursorclass=MySQLdb.cursors.DictCursor, 
      charset='utf8', 
      use_unicode=True 
     ) 

    def process_item(self, spider, item): 
     # run db query in thread pool 
     query = self.dbpool.runInteraction(self._conditional_insert, item) 
     query.addErrback(self.handle_error) 

     return item 

    def _conditional_insert(self, tx, item): 
     # create record if doesn't exist. 
     # all this block run on it's own thread 
     tx.execute("select * from scrapytest where link = %s", (item['link'][0],)) 
     result = tx.fetchone() 
     if result: 
      log.msg("Item already stored in db: %s" % item, level=log.DEBUG) 
     else: 
      tx.execute(\ 
       "insert into scrapytest (title, link, desc) " 
       "values (%s, %s, %s)", 
       (item['title'][0], 
       item['link'][0], 
       item['desc'][0]) 
      ) 
      log.msg("Item stored in db: %s" % item, level=log.DEBUG) 

    def handle_error(self, e): 
     log.err(e) 

나는 (Json과 같은) 다른 파이프 라인을 사용하려고 시도했지만 작동하지만 여기에는 내가 이해하지 못하는 일종의 수입 문제가 있습니다.

+0

파이프 라인 코드를 올바르게 붙여 넣은 것 같지 않습니다. 들여 쓰기를보고 클래스 MySQLStore가 비어 있습니다. – JosefAssad

+0

예, 코드를 붙여 넣을 때 약간 부주의했습니다. 그러나 지금은 수정했는데, 들여 쓰기가 올바른 원래 스크립트에서 문제가 있다고 생각하지 않습니다. 다른 제안? @JosefAssad – user1009453

답변

2

오류 메시지는 입니다. 매우입니다.

ImportError: ... No module named exceptions 

exceptions을 가져 오려고하는데 해당 모듈이 없습니다. 해당 모듈을 가져올 경우 코드를 살펴 :

from scrapy.core.exceptions import DropItem 

이 있어야한다 :

from scrapy.exceptions import DropItem 

는 더 scrapy.core.exceptions 모듈이 없습니다.

+0

위대한, 이걸 도와 주셔서 감사합니다. 당신이 제안한대로했는데 문제가 사라진 것처럼 보입니다. 하지만 지금받을 : "ImportError : 파일 이름으로 가져 오기가 지원되지 않습니다." 관련 문제입니까 아니면 완전히 다른 문제입니까? 다시 도움을 주셔서 감사합니다! – user1009453