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

标签 python python-2.7 selenium selenium-webdriver scrapy

我正在使用 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/

相关文章:

c# - Selenium C# 在表中查找链接

internet-explorer - Internet Explorer 中的 Selenium WebDriver 单击问题

python - 尝试发送电子邮件 Python 时出错

python - 在 Entry 小部件中重新绑定(bind) “select all”

python - 如果所有值都是某个字符串,则删除 pandas 数据框中的列

python - 在 python、bash 或 perl 中,如何人为地增加内存

python - 导入错误: cannot import name certificate_transparency after installing Pusher

python - 如何更改标签内某些单词的颜色?

python - 用python编码元组列表?

java - Selenium Webdriver - 在 CSS 定位器中使用存储的字符串