如何删除特殊字符并删除字符之间的空格?

时间:2015-01-14 10:08:59

标签: python python-2.7 selenium selenium-webdriver scrapy

我在scrapy + python工作。我试图从工作网址中提取jobid数据,任何人都可以指导我提取。

http://xxxxx/apply/EkhIMG/Director-Financial-Planning-Analysis.html

我必须单独提取这些内容" Director-Financial-Planning-Analysis"

还需要删除DirectorFinancialPlanningAnalysis的特殊字符

我的预期输出应为:DirectorFinancialPlanningAnalysis

我的蜘蛛代码是:

hxs = Selector(response) 
item = response.request.meta['item']
item ['JobDetailUrl'] = response.url
item ['InternalJobId'] = item ['JobDetailUrl'].re('.*\/(.*?)\.html').groups()

我的输出错误:

item ['InternalJobId'] = item['JobDetailUrl'].re('.*\/(.*?)\.html')
.groups()
exceptions.AttributeError: 'str' object has no attribute 're'

1 个答案:

答案 0 :(得分:1)

re()Selector对象上的方法,此处response.url是一个字符串:

re.search(r'([a-zA-Z\-]+)\.html$', response.url).group(1).replace('-', '')

演示:

>>> import re
>>> s = 'http://xxxxx/apply/EkhIMG/Director-Financial-Planning-Analysis.html'
>>> re.search(r'([a-zA-Z\-]+)\.html$', s).group(1).replace('-', '')
'DirectorFinancialPlanningAnalysis'
相关问题