Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用strip()删除空白_Python_Scrapy - Fatal编程技术网

Python 使用strip()删除空白

Python 使用strip()删除空白,python,scrapy,Python,Scrapy,如何删除[u'\n\n\n此处的结果\n\n'] 并得到一个结果作为[u'result here']仅。。。我用的是刮痧 def parse_items(self, response): str = "" hxs = HtmlXPathSelector(response) for titles in titles: item = CraigslistSampleItem() item ["job_id"] = (id.select('text()').ext

如何删除
[u'\n\n\n此处的结果\n\n']
并得到一个结果作为
[u'result here']
仅。。。我用的是刮痧

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() 
返回包含文本的字符串列表。您应该迭代该列表以去除每个项目,或者使用切片(例如,您的_列表[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()

可以在选择“作业id”的XPath表达式周围使用XPath函数
normalize-space()

注意1:我使用的XPath表达式基于

使用
.strip()
在答案上注2:使用
id.select('text()').extract()[0]。strip()
您在这里得到的是
u'result'
,而不是列表

这可能正是您所需要的,但是如果您希望保留列表,当您要求删除
[u'\n\n\n result here\n\n']
并得到一个结果作为
[u'result here']
,您可以使用类似的东西,使用Python的
映射()


u'\n\n\n result here\n\n\n'.strip()
应该可以。我用this=item[“job\u id”]=(id.select('text()).extract().strip()尝试了它,但它给了我一个error@chano这里的
id
是什么?错误是什么?(最好使用
id
作为变量名,
id()
是内置函数)项[“作业id”]=(id.select('text()).extract().strip()上述语法的结果是“exceptions.AttributeError:'list'对象没有属性'strip'
项[“job_id”]=[i.strip()for i in id.select('text()).extract()]
另一个问题…我创建了一个类似denier的路径_deny_base=['(登录名),(介绍名),(候选名),(参考名)“,”(提醒)”,“#”(\/search)”,这可能必须拒绝使用前面提到的字符串的页面,但我的刮板仍然像我的规则一样抓取页面:rules=(规则(sgmlLinkedExtractor(deny=path\u deny\u base,allow=(“*”),callback=“parse\u items”,follow=True),)@chano,你可能应该问另一个关于StackOverflow的问题,关于这个规则的拒绝问题,rry..我刚到这个网站..谢谢你提醒我…我检查了我的csv文件,发现这里仍然有一个结果…为什么是这样?谢谢你一千次!没有什么比刮复杂和不规则的东西更让我生气的了在搜索了一个小时左右后,我碰巧用您答案的一部分解决了我的问题。您也可以使用just.get().strip()而不使用[0]
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)
item ["job_id"] = map(unicode.strip, id.select('text()').extract())