2015-07-10 6 views
7

Ich habe einen sehr einfachen Code, unten gezeigt. Scraping ist in Ordnung, ich kann alle print Anweisungen sehen, die korrekte Daten erzeugen. In Pipeline funktioniert die Initialisierung einwandfrei. Die Funktion wird jedoch nicht aufgerufen, da die Anweisung print am Anfang der Funktion niemals ausgeführt wird.Python, Scrapy, Pipeline: Funktion "process_item" wird nicht aufgerufen

Spider: comosham.py

import scrapy 
from scrapy.spider import Spider 
from scrapy.selector import Selector 
from scrapy.http import Request 
from activityadvisor.items import ComoShamLocation 
from activityadvisor.items import ComoShamActivity 
from activityadvisor.items import ComoShamRates 
import re 


class ComoSham(Spider): 
    name = "comosham" 
    allowed_domains = ["www.comoshambhala.com"] 
    start_urls = [ 
     "http://www.comoshambhala.com/singapore/classes/schedules", 
     "http://www.comoshambhala.com/singapore/about/location-contact", 
     "http://www.comoshambhala.com/singapore/rates-and-offers/rates-classes", 
     "http://www.comoshambhala.com/singapore/rates-and-offers/rates-classes/rates-private-classes" 
    ] 

    def parse(self, response): 
     category = (response.url)[39:44] 
     print 'in parse' 
     if category == 'class': 
      pass 
      """self.gen_req_class(response)""" 
     elif category == 'about': 
      print 'about to call parse_location' 
      self.parse_location(response) 
     elif category == 'rates': 
      pass 
      """self.parse_rates(response)""" 
     else: 
      print 'Cant find appropriate category! check check check!! Am raising Level 5 ALARM - You are a MORON :D' 


    def parse_location(self, response): 
     print 'in parse_location'  
     item = ComoShamLocation() 
     item['category'] = 'location' 
     loc = Selector(response).xpath('((//div[@id = "node-2266"]/div/div/div)[1]/div/div/p//text())').extract() 
     item['address'] = loc[2]+loc[3]+loc[4]+(loc[5])[1:11] 
     item['pin'] = (loc[5])[11:18] 
     item['phone'] = (loc[9])[6:20] 
     item['fax'] = (loc[10])[6:20] 
     item['email'] = loc[12] 
     print item['address'],item['pin'],item['phone'],item['fax'],item['email'] 
     return item 

Artikel Datei:

import scrapy 
from scrapy.item import Item, Field 

class ComoShamLocation(Item): 
    address = Field() 
    pin = Field() 
    phone = Field() 
    fax = Field() 
    email = Field() 
    category = Field() 

Pipeline-Datei:

class ComoShamPipeline(object): 
    def __init__(self): 
     self.locationdump = csv.writer(open('./scraped data/ComoSham/ComoshamLocation.csv','wb')) 
     self.locationdump.writerow(['Address','Pin','Phone','Fax','Email']) 


    def process_item(self,item,spider): 
     print 'processing item now' 
     if item['category'] == 'location': 
      print item['address'],item['pin'],item['phone'],item['fax'],item['email'] 
      self.locationdump.writerow([item['address'],item['pin'],item['phone'],item['fax'],item['email']]) 
     else: 
      pass 
+0

Wird ein Element am Ende der Funktion 'parse_location' generiert und hat es Werte? – GHajba

+0

Ja, am Ende von 'parse_location' drucke ich es und die Ausgabe ist wie erwartet. –

+0

Ich denke du hast es aber ich muss es fragen: hast du die ItemPipeline im 'settings.py' konfiguriert? – GHajba

Antwort

9

Ihr Problem ist, dass Sie eigentlich nie das Element erhalten wurde. parse_location gibt ein Element zurück, das analysiert werden soll, parse liefert dieses Element jedoch nie.

Die Lösung wäre zu ersetzen:

self.parse_location(response) 

mit

yield self.parse_location(response) 

Insbesondere process_item nie aufgerufen wird, wenn keine Elemente nachgegeben werden.

1

Verwenden ITEM_PIPELINES in settings.py:

ITEM_PIPELINES = ['project_name.pipelines.pipeline_class'] 
0

auf die Antworten oben Hinzufügen
1. Denken Sie daran, die folgende Zeile in den settings.py! ITEM_PIPELINES = {'[YOUR_PROJECT_NAME].pipelines.[YOUR_PIPELINE_CLASS]': 300} 2. Geben Sie den Gegenstand, wenn Ihre Spinne läuft! yield my_item

+0

korrigieren Sie ['YOUR_PROJECT_NAME] zu' [YOUR_PROJECT_NAME] –

0

Dies löste mein Problem: ich alle Artikel wurde Dropping vor meiner Pipeline aufgerufen, so process_item() wurde nicht genannt zu werden, aber open_spider und close_spider genannt wurde. Also tmy Lösung war nur ändern Sie die Reihenfolge, um diese Pipeline vor der anderen Pipeline, die Elemente löscht. erinnern

Scrapy Pipeline Documentation.

Nur, dass Scrapy ruft Pipeline.process_item() nur dann, wenn Artikel ist zu verarbeiten!

Verwandte Themen