python - Write text file to pipeline -


i have multiple spiders in single scrapy project.

i want write separate output text file each spider spider name , time stamp.

when had single spider creating file in __init method trying this, upromise generate 2 output files while other one.

class mallcrawlerpipeline(object):  def spider_opened(self, spider):     self.awriter = csv.writer(open('../%s_%s.txt' % (spider.name, datetime.now().strftime("%y%m%d_%h%m%s")), 'wb'),         delimiter=',', quoting=csv.quote_minimal)     self.awriter.writerow(['mall', 'store', 'bonus', 'per_action', 'more_than','up_to', 'deal_url', 'category'])      if 'upromise' in spider.name:         self.cwriter = csv.writer(             open('../%s_coupons_%s.txt' % (spider.name, datetime.now().strftime("%y%m%d_%h%m%s")), 'wb'),             delimiter=',', quoting=csv.quote_minimal)         self.cwriter.writerow(['mall', 'store', 'bonus', 'per_action', 'more_than','up_to', 'deal_url', 'category'])  def process_item(self, item, spider):     self.awriter.writerow([item['mall'], item['store'], item['bonus'], item['per_action'],                            item['more_than'], item['up_to'], item['deal_url'], item['category']])      return item 

but facing bug:

 file "c:\users\akhter\dropbox\akhter\mall_crawler\mall_crawler\pipelines.py", line 24, in process_item     self.awriter.writerow([item['mall'], item['store'], item['bonus'], item['per_action'], exceptions.attributeerror: 'mallcrawlerpipeline' object has no attribute 'awriter' 

any appreciated. in advance.

are sure you're running obj.spider_opened(...) before obj.process_item(...)? seems you're not, after first method call attribute should have been added object.

if first method call needed perhaps makes sense move __init__ or call there, way.


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -