簡體   English   中英

使用strip()刪除空白

[英]remove white space using strip()

我怎樣才能刪除[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

誰能幫我?

id.select('text()').extract() 

返回包含您的文本的字符串列表。 您應該遍歷該列表以剝離每個項目,或者使用切片例如your_list [0] .strip()進行剝離空白。 Strip方法實際上與字符串數據類型相關聯。

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

  for titles in titles:
      item = CraigslistSampleItem()
      item ["job_id"] = id.select('text()').extract()[0].strip() #this should work if #there is some string data available. otherwise it will give an index out of range error.
      items.append(item)
  return(items)
end

替代使用Python的.strip()

您可以在選擇“ job_id”的XPath表達式周圍使用XPath函數normalize-space()

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)

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

注解2中使用.strip()的答案 :與id.select('text()').extract()[0].strip()您得到的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())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM