Python scrapy:使用变量加载项
你好,我是新的刮,我需要加载2个日期在一个网站上。如何将变量放入项目中Python scrapy:使用变量加载项,python,variables,scrapy,load,Python,Variables,Scrapy,Load,你好,我是新的刮,我需要加载2个日期在一个网站上。如何将变量放入项目中 fecha_today = datetime.date.today().strftime("%d-%m-%y") fecha_yesterday = (datetime.date.today()- timedelta(1)).strftime("%d-%m-%y") 这是我的蜘蛛 def parse_date(self, response): self.log("\n\n\n ponemos las fechas
fecha_today = datetime.date.today().strftime("%d-%m-%y")
fecha_yesterday = (datetime.date.today()- timedelta(1)).strftime("%d-%m-%y")
这是我的蜘蛛
def parse_date(self, response):
self.log("\n\n\n ponemos las fechas \n\n\n")
hxs = HtmlXPathSelector(response)
link_fecha = hxs.select('/html/body/table/tbody/tr[3]/td/a')
date_item= ItemLoader ( FechaItem ())
date_item.add_path('fecha_today','/html/body/table[1]/tbody/tr[2]/td/form/table/tbody/tr[3]/td[1]/span/input')
date_item.add_path('fecha_yesterday','/html/body/table[1]/tbody/tr[2]/td/form/table/tbody/tr[4]/td[1]/span/input')
return date_item.load_item()
我必须在item.py中放入什么才能让变量接受我?
item.py
我需要放置这些变量,然后访问一个表
根本不需要使用项目加载器 我已经用Python Scrapy编写代码3年多了,我从来没有使用过它,只是简单地生成了一个这样的字典
def parse_date(self, response):
self.log("\n\n\n ponemos las fechas \n\n\n")
item = {}
item['fecha_today'] = response.xpath('/html/body/table[1]/tbody/tr[2]/td/form/table/tbody/tr[3]/td[1]/span/input').extract_first()
item['fecha_yesterday'] = response.xpath('/html/body/table[1]/tbody/tr[2]/td/form/table/tbody/tr[4]/td[1]/span/input').extract_first()
yield item
但是,如何放置变量以访问表呢?我必须在课堂上写些什么?我理解你的代码,但是我如何在网上写上昨天和今天与scrapy的日期呢?我不必提取数据,但把它放在你想提取的日期从刮页?只需获取Xpath并执行response.Xpath(“Xpath here”).extract_first()。。。这就是我需要把这些变量放到一个表中。用字段的图片修改主题
def parse_date(self, response):
self.log("\n\n\n ponemos las fechas \n\n\n")
item = {}
item['fecha_today'] = response.xpath('/html/body/table[1]/tbody/tr[2]/td/form/table/tbody/tr[3]/td[1]/span/input').extract_first()
item['fecha_yesterday'] = response.xpath('/html/body/table[1]/tbody/tr[2]/td/form/table/tbody/tr[4]/td[1]/span/input').extract_first()
yield item