【问题标题】:How to remove special charactor and remove space between charactors?如何删除特殊字符并删除字符之间的空格?
【发布时间】:2015-03-12 11:36:43
【问题描述】:

我正在使用scrapy + python。我试图从job url中提取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'

【问题讨论】:

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


    【解决方案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'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-01
      • 2018-04-24
      • 2020-03-16
      • 2020-07-29
      • 1970-01-01
      • 1970-01-01
      • 2021-10-18
      相关资源
      最近更新 更多