So che ci sono diversi thread correlati là fuori, e mi hanno aiutato molto, ma non riesco ancora ad arrivare fino in fondo. Sono al punto in cui l'esecuzione del codice non comporta errori, ma non ottengo nulla nel mio file csv
. Ho la Scrapy
ragno successivo inizia in una pagina web, poi segue un collegamento ipertestuale, e raschia la pagina collegata:Seguendo il collegamento ipertestuale e "Richiesta offsite filtrata"
from scrapy.http import Request
from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from scrapy.item import Item, Field
class bbrItem(Item):
Year = Field()
AppraisalDate = Field()
PropertyValue = Field()
LandValue = Field()
Usage = Field()
LandSize = Field()
Address = Field()
class spiderBBRTest(BaseSpider):
name = 'spiderBBRTest'
allowed_domains = ["http://boliga.dk"]
start_urls = ['http://www.boliga.dk/bbr/resultater?sort=hus_nr_sort-a,etage-a,side-a&gade=Septembervej&hus_nr=29&ipostnr=2730']
def parse2(self, response):
hxs = HtmlXPathSelector(response)
bbrs2 = hxs.select("id('evaluationControl')/div[2]/div")
bbrs = iter(bbrs2)
next(bbrs)
for bbr in bbrs:
item = bbrItem()
item['Year'] = bbr.select("table/tbody/tr[1]/td[2]/text()").extract()
item['AppraisalDate'] = bbr.select("table/tbody/tr[2]/td[2]/text()").extract()
item['PropertyValue'] = bbr.select("table/tbody/tr[3]/td[2]/text()").extract()
item['LandValue'] = bbr.select("table/tbody/tr[4]/td[2]/text()").extract()
item['Usage'] = bbr.select("table/tbody/tr[5]/td[2]/text()").extract()
item['LandSize'] = bbr.select("table/tbody/tr[6]/td[2]/text()").extract()
item['Address'] = response.meta['address']
yield item
def parse(self, response):
hxs = HtmlXPathSelector(response)
PartUrl = ''.join(hxs.select("id('searchresult')/tr/td[1]/a/@href").extract())
url2 = ''.join(["http://www.boliga.dk", PartUrl])
yield Request(url=url2, meta={'address': hxs.select("id('searchresult')/tr/td[1]/a[@href]/text()").extract()}, callback=self.parse2)
sto cercando di esportare i risultati in un file CSV, ma non ottengo niente il file. L'esecuzione del codice, tuttavia, non comporta errori. So che è un esempio semplicemente semplice con un solo URL, ma illustra il mio problema.
Penso che il mio problema potrebbe essere che non sto dicendo a Scrapy
che voglio salvare i dati nel metodo Parse2
.
BTW, corro il ragno come scrapy crawl spiderBBR -o scraped_data.csv -t csv
Fa 'parse2' viene chiamato - Non riesco a vedere da dove provenga. Non sembra che ci sia qualcosa che cerca di scrivere in un file CSV. –
@SteveAllison: Ups, è un errore di battitura. L'ho modificato in modo che chiamo 'parse2' come callback nella richiesta in' parse', ma ancora non funziona. – Mace