2016-08-07 5 views
0

나는 긁힌 데이터를 mysql 데이터베이스에 저장하기 위해 치료 과정에서 파이프 라인을 만들고 있습니다. 거미가 터미널에서 작동하면 완벽하게 작동합니다. 파이프 라인조차 열립니다. 그러나 데이터가 데이터베이스로 보내지지 않습니다. 어떤 도움을 주셔서 감사합니다! :)치료 파이프 라인이 데이터를 MySQL에 삽입하지 않습니다

여기에 파이프 라인의 코드입니다 :

import scrapy 

from scrapy.pipelines.images import ImagesPipeline 
from scrapy.exceptions import DropItem 
from scrapy.http import Request 

import datetime 
import logging 
import MySQLdb 
import MySQLdb.cursors 
from scrapy.exceptions import DropItem 
from en_movie.items import EnMovie 



class DuplicatesPipeline(object): 
    def __init__(self): 
     self.ids_seen = set() 

    def process_item(self, item, spider): 
     if item['image_urls'] in self.ids_seen: 
      raise DropItem("Duplicate item found: %s" % item) 
     else: 
      self.ids_seen.add(item['image_urls']) 
      return item 



class MyImagesPipeline(ImagesPipeline): 
    def get_media_requests(self, item, info): 
     for image_url in item['image_urls']: 
      yield scrapy.Request(image_url) 

    def item_completed(self, results, item, info): 
     image_paths = [x['path'] for ok, x in results if ok] 
     if not image_paths: 
      raise DropItem("Item contains no images") 
     item['image_paths'] = ', '.join(image_paths) 
     return item 



class EnMovieStorePipeline(object): 
    def __init__(self): 

     self.conn = MySQLdb.connect(host="localhost", user="root",  passwd="pass", db="passdb", charset="utf8", use_unicode=True) 
     self.cursor = self.conn.cursor() 



    def process_item(self, item, spider): 
     cursor.execute("""SELECT * FROM dmmactress_enmovielist WHERE Content_ID = %s and release_date = %s and running_time = %s and Actress = %s and Series = %s and Studio = %s and Director = %s and Label = %s and image_paths = %s and image_urls = %s""", 
     (item['Content_ID'][0].encode('utf-8'), item['release_date'][0].encode('utf-8'), item['running_time'][0].encode('utf-8'), item['Actress'][0].encode('utf-8'), item['Series'][0].encode('utf-8'), item['Studio'][0].encode('utf-8'), item['Director'][0].encode('utf-8'), item['Label'][0].encode('utf-8'), item['image_urls'][0].encode('utf-8'))) 
     result = self.cursor.fetchone() 


     if result: 
      print("data already exist") 
     else: 
      try: 
       cursor.execute("""INSERT INTO dmmactress_enmovielist(Content_ID, release_date, running_time, Actress, Series, Studio, Director, Label, image_paths, image_urls) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""", 
       (item['Content_ID'][0].encode('utf-8'), item['release_date'][0].encode('utf-8'), item['running_time'][0].encode('utf-8'), item['Actress'][0].encode('utf-8'), item['Series'][0].encode('utf-8'), item['Studio'][0].encode('utf-8'), item['Director'][0].encode('utf-8'), item['Label'][0].encode('utf-8'), item['image_urls'][0].encode('utf-8'))) 
       self.conn.commit() 
      except MySQLdb.Error as e: 
       print ("Error %d: %s" % (e.args[0], e.args[1])) 
       return item 

편집 :

def parse_item(self, response): 
    for sel in response.xpath('//*[@id="contents"]/div[10]/section/section[1]/section[1]'): 
     item = EnMovie() 
     Content_ID = sel.xpath('normalize-space(div[2]/dl/dt[contains (.,"Content ID:")]/following-sibling::dd[1]/text())').extract() 
     item['Content_ID'] = Content_ID[0].encode('utf-8') 
     release_date = sel.xpath('normalize-space(div[2]/dl[1]/dt[contains (.,"Release Date:")]/following-sibling::dd[1]/text())').extract() 
     item['release_date'] = release_date[0].encode('utf-8') 
     running_time = sel.xpath('normalize-space(div[2]/dl[1]/dt[contains (.,"Runtime:")]/following-sibling::dd[1]/text())').extract() 
     item['running_time'] = running_time[0].encode('utf-8') 
     Series = sel.xpath('normalize-space(div[2]/dl[2]/dt[contains (.,"Series:")]/following-sibling::dd[1]/text())').extract() 
     item['Series'] = Series[0].encode('utf-8') 
     Studio = sel.xpath('normalize-space(div[2]/dl[2]/dt[contains (.,"Studio:")]/following-sibling::dd[1]/a/text())').extract() 
     item['Studio'] = Studio[0].encode('utf-8') 
     Director = sel.xpath('normalize-space(div[2]/dl[2]/dt[contains (.,"Director:")]/following-sibling::dd[1]/text())').extract() 
     item['Director'] = Director[0].encode('utf-8') 
     Label = sel.xpath('normalize-space(div[2]/dl[2]/dt[contains (.,"Label:")]/following-sibling::dd[1]/text())').extract() 
     item['Label'] = Label[0].encode('utf-8') 
     item['image_urls'] = sel.xpath('div[1]/img/@src').extract() 

     actresses = sel.xpath("//*[@itemprop='actors']//*[@itemprop='name']/text()").extract() 
     actress = [x.strip() for x in actresses] 
     item['Actress'] = ", ".join(actress) 
     yield item 
+0

당신이 오류를 받으셨어요 : 당시 나는이 오프 몇 가지 코드를 기반으로? '아이템'이 어떻게 보이는지 보여 줄 수 있습니까? – Harrison

+0

안녕하세요. 해리슨. 나는 어떤 오류도 내지 않았다. 그래서, 나는 정말로 내 파이프 라인에 무엇이 잘못되었는지를 모른다. (귀하의 회신에 대해 내 게시물을 수정했습니다) 감사합니다. – Jin

+0

대부분의'item' 필드는'image_urls'를 제외하고는 문자열입니다. item [ 'image_urls']'를 제외한'item_ 'Content_ID']','item [ 'release_date']'등의 뒤에'[0]'을 제거해보십시오. – Harrison

답변

0

나는이 작업을 몇 주 전에 가지고 있지만 다른 데이터베이스로 전환. https://gist.github.com/tzermias/6982723 또한

뿐만 아니라 위의 코드를 사용하는 경우 settings.py 파일을 업데이트하는 것을 잊지 ...

+0

죄송합니다. 링크 된 코드가 mysql에서 작동했음을 추가해야합니다. – Mike77

관련 문제