使用 strip() 删除空格

cha*_*ano 0 python scrapy

我怎样才能删除[u'\n\n\n result here \n\n\n'] 并得到结果[u'result here']...我正在使用 scrapy

def parse_items(self, response):
  str = ""
  hxs = HtmlXPathSelector(response)

  for titles in titles:
      item = CraigslistSampleItem()
      item ["job_id"] = (id.select('text()').extract() #ok
      items.append(item)
  return(items)
end
Run Code Online (Sandbox Code Playgroud)

谁能帮我?

pau*_*rth 5

使用 Python 的替代方案.strip()

normalize-space()您可以在选择“job_id”的 XPath 表达式周围使用 XPath 函数:

def parse_items(self, response):
    hxs = HtmlXPathSelector(response)

    for titles in titles:
        item = CraigslistSampleItem()
        item ["job_id"] = title.select('normalize-space(.//td[@scope="row"])').extract()[0].strip()
        items.append(item)
    return(items)
Run Code Online (Sandbox Code Playgroud)

注1:我使用的XPath表达式基于https://careers-cooperhealth.icims.com/jobs/search?ss=1&searchLocation=&searchCategory=&hashed=0

注意 2 答案使用.strip(): with id.select('text()').extract()[0].strip()you get u'result here',而不是列表。

这很可能正是您所需要的,但是如果您想保留列表,正如您要求删除[u'\n\n\n result here \n\n\n']并获得结果一样[u'result here'],您可以使用类似这样的东西,使用Python的map()

item ["job_id"] = map(unicode.strip, id.select('text()').extract())
Run Code Online (Sandbox Code Playgroud)