我正在使用 scrapy + python 工作。我尝试从工作网址中提取 jobid 数据,任何人都可以指导我提取此数据。
http://xxxxx/apply/EkhIMG/Director-Financial-Planning-Analysis.html
我必须单独提取此内容“总监-财务-规划-分析”
还需要删除特殊字符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'
最佳答案
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'
关于python - 如何删除特殊字符并删除字符之间的空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27940352/