scrapy正则表达式无法找到长破折号

2024-10-01 17:33:42 发布

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

我使用scrapy xpath+re从web页面提取数据。字符是unicode(俄语),所有要提取的字符串都包含长破折号(python代码'\u2014') 问题是我的正则表达式找不到完整的字符串并用长破折号分割它。对我来说真的很不方便。 下面是一些我已经尝试过但没有成功的例子:

response.xpath('some xpath goes here').re(r'[\w\s\\u2014\.,]+')
response.xpath('some xpath goes here').re(r'[\w\s\\u2014\.,]+')
response.xpath('some xpath goes here').re(r'[\w\s\\x2014\.,]+')
response.xpath('some xpath goes here').re(r'[\w\s\\uFFFF\.,]+')
response.xpath('some xpath goes here').re(r'[\w\s\.,—]+')
response.xpath('some xpath goes here').re(r'[\w\s\u(\w){4}\.,]+')
response.xpath('some xpath goes here').re(r'[\w\s(\u(\d)){6}\.,]+')

版本:Python2.7,Scrapy 0.24.6


Tags: 数据字符串rewebhereresponseunicodesome
1条回答
网友
1楼 · 发布于 2024-10-01 17:33:42

将模式转换为unicode字符串,不要转义\。在

response.xpath('some xpath goes here').re(ur'[\w\s\u2014\.,]+')

另外,我想您可能希望使用^{}标志,这样\w和{}将匹配所有Unicode单词和空白字符。根据Scrapy文档,^{}不支持标志,但它可以使用已编译的正则表达式,因此yy可以执行以下操作:

^{pr2}$

相关问题 更多 >

    热门问题