def parse_item(self, response): item_loader = NewItemLoader(NewItem(), response) item_loader.add_xpath('title', '//div[@id="chan_newsBlk"]/h1/text()') item_loader.add_xpath('time', '//div[@id="chan_newsInfo"]/text()', re='(\d+-\d+-\d+\s\d+:\d+:\d+)') item_loader.add_value('url', response.url) item_loader.add_xpath('text', '//div[@id="chan_newsDetail"]//text()') item_loader.add_xpath('source', '//div[@id="chan_newsInfo"]/text()', re='来源:(.*)')
在用到itemloader时,既可以通过在item中定义一些函数,作为input_processor()的参数对提取的数据进行修改,也可以直接在提取的字段后面加入自己的正则表达式提取数据。如上图中的re语句。