我怎样才能删除[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)
谁能帮我?
使用 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)
| 归档时间: |
|
| 查看次数: |
11067 次 |
| 最近记录: |