【问题标题】:scrapy: load item with variablesscrapy:使用变量加载项目
【发布时间】:2018-06-15 20:37:37
【问题描述】:

你好我是scrapy的新手,我需要在网上加载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 \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 中放入什么才能让变量带走我? 项目.py

class    FechaLoader(scrapy.loader.ItemLoader):

我需要把这些变量放到一个表中去

enter image description here

【问题讨论】:

    标签: python variables scrapy load


    【解决方案1】:

    根本不需要使用物品加载器

    我用 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() ... 就是这样
    • 我需要把这些变量放到一个表中。用字段的图片修改主题
    猜你喜欢
    • 2016-10-08
    • 2014-10-03
    • 1970-01-01
    • 2022-01-22
    • 2019-02-18
    • 2018-07-30
    • 2020-10-18
    • 2018-03-19
    • 2020-07-24
    相关资源
    最近更新 更多