response.replace(body=response.text.replace(‘xa0‘,‘‘)),scrapy抓取网页含r t n xa0时,修改response方法

    技术2022-07-17  66

    xpath抓取数据值有\r\n\t时去掉的方法:https://blog.csdn.net/z564359805/article/details/101597953

    抓取网页含\r \t \n时,用normalize-space出现特殊符号有时候并不会成功,例如:['商家 \xa0厦门有限公司'],'\xa0'在网页源码中是' ',可以用如下方法:

    方法一:修改response这种方法是修改网页代码里面的数据,'\xa0'在网页源码中是' ',个人觉得毕竟不是筛选后的数据,修改时间会比较长

    def parse(self,response): # 修改网页代码里面的数据 response = response.replace(body=response.text.replace(' ','')) order_company = response.xpath('normalize-space(//*[@id="to"]/tbody/tr/td[3]/a/text())').extract() item['order_company'] = order_company[0].strip()

    方法二:在选择出需要的item数据传递时候直接替换

    item['order_company'] = order_company[0].replace("\xa0", "").strip()

     

    Processed: 0.013, SQL: 9