【问题标题】:ValueError('Cannot process flags argument with a compiled pattern')ValueError('无法处理带有编译模式的标志参数')
【发布时间】:2014-08-04 21:39:52
【问题描述】:

我在 Windows Vista 64 位上使用 Python.org 版本 2.7 64 位。我正在使用带有 Scrapy 的正则表达式来解析以下页面中名为“Datastore.prime”的第一个 Javascript 项中包含的数据:

Link here 我使用的正则表达式是这样的:

regex = re.compile('DataStore\.prime\(\'standings\', { stageId: \d+ }.*')
        match2 = re.findall(regex, response.body, re.S)
        match3 = str(match2)
        match3 = match3.replace('<a class="w h"', '').replace('<a class="w a"', '').replace('<a class="d h"', '') \
                 .replace('<a class="d a"', '').replace('<a class="l h"', '').replace('<a class="l a"', '') \
                 .replace('title=', '')
        print match3

但是,这会在这篇文章的标题中抛出错误:

     raise ValueError('Cannot process flags argument with a compiled pattern')
 exceptions.ValueError: Cannot process flags argument with a compiled pattern

谁能看出问题出在哪里?

谢谢

【问题讨论】:

    标签: python regex scrapy


    【解决方案1】:

    您需要将修饰符放在re.compile 模式中,在这里使用原始字符串表示法也很好。

    regex = re.compile(r"DataStore\.prime\('standings', { stageId: \d+ }.*", re.S)
                                                                             ^^^^
    

    【讨论】:

    • 你好。从某种意义上说,代码不再出错,但它并没有只选择我现在想要的输出。我最初只是使用 regex = r'[[.*?]]);' 的正则表达式,它返回页面上两组方括号内的所有内容。我想要的只是页面上 FIRST datastore.prime 项目中的所有内容,这段代码应该提供这些内容,但现在我回到了我开始的地方......
    • 上例中的转义反斜杠,但由于某种原因它们没有显示
    • 现在什么都不返回了。
    • 顺便说一句,这回答了您关于为什么会出错的问题,Stackoverflow 不是代码编写服务,所以如果您在其他方面遇到更多问题,请提出一个新问题。
    猜你喜欢
    • 2013-11-10
    • 1970-01-01
    • 2021-08-22
    • 2019-08-06
    • 1970-01-01
    • 2020-04-14
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    相关资源
    最近更新 更多