首页 > XPath匹配时如何去掉空格换行只保留文本内容

XPath匹配时如何去掉空格换行只保留文本内容

用XPath提取后是下面的样式,想把\r\n去除掉,只保留 2015年 这一段文本,请问该如何操作?

\r\n                       \r\n                            \r\n                            \r\n                                2015\u5e74\r\n                            \r\n                        \r\n                          \r\n                              

查询 normalize-space 可以去除空格,但是这么写,什么都没有匹配出来,是写错了,还是并不能用这个函数

item['title'] = sel.xpath('a[normalize-space(//text())]').extract()

个人建议你使用一下 ItemLoader
l = ItemLoader(item=YourItem(), response=response)
l.add_xpath('title','//text()',MapCompose(unicode.strip, unicode.title))
return l.load_item()

【热门文章】
【热门文章】