最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

【已解决】Python中使用re.search出错:sre_constants.error: syntax error

Python re crifan 7730浏览 0评论

【问题】

在Python中,试图用:

foundMainUrlOrPermalink = re.search("(?P<blogIdMainUrl>http://blog\.tianya\.cn/blogger/blog_main\.asp\?BlogID=(?P<blogId>\d+))(&PostID=(?<postId>\d+))?/?", inputUrl);

去匹配:

    # type2, main url with blogId, perma link:
    # http://blog.tianya.cn/blogger/blog_main.asp?BlogID=2723898
    # http://blog.tianya.cn/blogger/blog_main.asp?BlogID=2723898/
    # http://blog.tianya.cn/blogger/post_read.asp?BlogID=2723898&amp;PostID=23024897
    # <a href="http://blog.tianya.cn/blogger/post_read.asp?BlogID=2723898&amp;PostID=23024897/">http://blog.tianya.cn/blogger/post_read.asp?BlogID=2723898&amp;PostID=23024897/</a>

结果出错:

Traceback (most recent call last):
  File "D:\tmp\tmp_dev_root\wordpress\to_wp\BlogsToWordpress\BlogsToWordpress\BlogsToWordpress.py", line 1816, in <module>
    main();
  File "D:\tmp\tmp_dev_root\wordpress\to_wp\BlogsToWordpress\BlogsToWordpress\BlogsToWordpress.py", line 1573, in main
    initialization(srcUrl);
  File "D:\tmp\tmp_dev_root\wordpress\to_wp\BlogsToWordpress\BlogsToWordpress\BlogsToWordpress.py", line 1408, in initialization
    (extractOK, extractedBlogUser, generatedBlogEntryUrl) = extractBlogUser(inputUrl);
  File "D:\tmp\tmp_dev_root\wordpress\to_wp\BlogsToWordpress\BlogsToWordpress\BlogsToWordpress.py", line 1772, in extractBlogUser
    return callBlogFunc(extractBlogUser, inputUrl);
  File "D:\tmp\tmp_dev_root\wordpress\to_wp\BlogsToWordpress\BlogsToWordpress\BlogsToWordpress.py", line 1681, in callBlogFunc
    return trueFunc(paraList[0]);
  File "libs/crifan/blogModules\BlogTianya.py", line 155, in extractBlogUser
    foundMainUrlOrPermalink = re.search("(?P<blogIdMainUrl>http://blog\.tianya\.cn/blogger/blog_main\.asp\?BlogID=(?P<blogId>\d+))(&PostID=(?<postId>\d+))?/?", inputUrl);
  File "D:\tmp\WordPress\DevRoot\Python27\lib\re.py", line 142, in search
    return _compile(pattern, flags).search(string)
  File "D:\tmp\WordPress\DevRoot\Python27\lib\re.py", line 244, in _compile
    raise error, v # invalid expression
sre_constants.error: syntax error

【解决过程】

1.调试了半天,最后竟然发现,原来是自己不小心,对于正则表达式中,少了个P,应该是:

foundMainUrlOrPermalink = re.search("(?P<blogIdMainUrl>http://blog\.tianya\.cn/blogger/blog_main\.asp\?BlogID=(?P<blogId>\d+))(&PostID=(?P<postId>\d+))?/?", inputUrl);

【总结】

看来写代码时,还是要慢点写,注意不要犯语法错误了。。。。

转载请注明:在路上 » 【已解决】Python中使用re.search出错:sre_constants.error: syntax error

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
76 queries in 0.105 seconds, using 18.31MB memory