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

2024-10-01 09:40:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我在scrapy+python中工作。我试过从jobid中提取jobid数据,任何人都可以指导我提取这个。你知道吗

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

我要单独摘录这篇内容“主任财务规划分析”

还需要删除特殊字符太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'

Tags: 数据rehttpresponsehtmlitemgroupsscrapy
1条回答
网友
1楼 · 发布于 2024-10-01 09:40:33

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'

相关问题 更多 >