This is my first time using scrapy and I'm trying to put the information I need into a csv file using the pipeline. Everything seemed to be working fine until I tried to scrape from more than one page and it started to return a blank csv file. I think the problem is in the spider (as when I made changes there it stopped working right), but I'm putting the pipeline up just in case there's something wrong in there also.
here's my spider:
import scrapy
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
from indeed.items import IndeedItem
class IndeedSpider(CrawlSpider):
name = "indeed"
allowed_domains = ['www.indeed.com']
start_urls = [
'http://www.indeed.com/jobs?as_and=&as_phr=&as_any=&as_not=&as_ttl=&as_cmp=&jt=all&st=&salary=&radius=50&l=19103&fromage=30&limit=10&sort=&psf=advsrch'
]
rules = (Rule(LinkExtractor(allow=('http://www.indeed.com/jobs?q=&l=19103&radius=50&fromage=30&start=.*'))), )
def parse_item(self, response):
for sel in response.xpath("//div[contains(@class, 'row ')]"):
items = []
jobs = sel.xpath('//a[contains(@data-tn-element, "jobTitle")]/text()').extract()
city = sel.xpath('//span[@class="location"]/text()').extract()
company = sel.xpath('//span[@class="company"]/text()').extract()
for j, c, co in zip(jobs, city, company):
position = IndeedItem()
position['jobs'] = j.strip()
position['city'] = c.strip()
position['company'] = co.strip()
items.append(position)
yield items
And here's my pipeline:
from scrapy import signals
from scrapy.xlib.pydispatch import dispatcher
from scrapy.exporters import CsvItemExporter
class IndeedPipeline(object):
def process_item(self, item, spider):
return item
class CsvExportPipeline(object):
def __init__(self):
dispatcher.connect(self.spider_opened, signals.spider_opened)
dispatcher.connect(self.spider_closed, signals.spider_closed)
self.files = {}
def spider_opened(self, spider):
file = open('%s_jobs.csv' % spider.name, 'w+b')
self.files[spider] = file
self.exporter = CsvItemExporter(file)
self.exporter.start_exporting()
def spider_closed(self, spider):
self.exporter.finish_exporting()
file = self.files.pop(spider)
file.close()
def process_item(self, item, spider):
self.exporter.export_item(item)
return item
Any help would be much appreciated.