ValueError('无法处理带有已编译模式的标志参数')

4 投票
1 回答
9229 浏览
提问于 2025-04-18 15:59

我在使用Windows Vista 64位系统上的Python.org 2.7 64位版本。我正在用Scrapy这个工具,通过正则表达式来解析以下页面中第一个名为'Datastore.prime'的Javascript项目里的数据:

点击这里我使用的正则表达式是:

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

有没有人能看出问题出在哪里?

谢谢

1 个回答

14

你需要把修饰符放在你的 re.compile 模式里面,另外这里最好使用原始字符串的写法。

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

撰写回答