事实证明,数据中还存在许多空格,因此结合Steven的回答和更多研究后,数据就可以删除所有标记,行返回和重复的空格。工作代码如下。请注意,在加载程序行上添加了text(),它删除了标记,并且split and join处理器删除了空格和行返回。
def parse(self, response): items = [] l = XPathItemLoader(item=Greenhouse(), response=response) l.default_input_processor = MapCompose(lambda v: v.split(), replace_escape_chars) l.default_output_processor = Join() l.add_xpath('title', '//h1/text()') l.add_xpath('usage', '//li[@id="ctl18_ctl00_rptProductAttributes_ctl00_liItem"]/text()') l.add_xpath('repeat', '//li[@id="ctl18_ctl00_rptProductAttributes_ctl02_liItem"]/text()') l.add_xpath('direction', '//li[@id="ctl18_ctl00_rptProductAttributes_ctl03_liItem"]/text()') items.append(l.load_item()) return items 


