免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: mattoid
打印 上一主题 下一主题

网页中开头的.*贪婪匹配为什么不成功呢 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2008-08-28 14:50 |只看该作者
解决了,谢谢楼上,加了个(?s)就可以了 。。
不过还不晓得(?s)是虾米意思

论坛徽章:
0
12 [报告]
发表于 2008-08-28 14:52 |只看该作者

回复 #11 mattoid 的帖子

具体我也不知道,从别人那学来的,呵呵

论坛徽章:
0
13 [报告]
发表于 2008-08-28 15:04 |只看该作者
可以看官方docs
(?iLmsux)
(One or more letters from the set "i", "L", "m", "s", "u", "x".) The group matches the empty string; the letters set the corresponding flags (re.I, re.L, re.M, re.S, re.U, re.X) for the entire regular expression. This is useful if you wish to include the flags as part of the regular expression, instead of passing a flag argument to the compile() function.
Note that the (?x) flag changes how the expression is parsed. It should be used first in the expression string, or after one or more whitespace characters. If there are non-whitespace characters before the flag, the results are undefined.

?s -> re.S

论坛徽章:
0
14 [报告]
发表于 2008-08-28 16:15 |只看该作者
参数放错了位置:


  1. #!/usr/bin/env python                                                           

  2. import urllib2
  3. import re

  4. url = 'http://www.baidu.com'
  5. body = urllib2.urlopen( url ).read()
  6. lastLink = re.compile(u'.*http(.*?)>'.encode('gbk'), re.I|re.S).search(body)
  7. print lastLink.group(1)
复制代码


[quote]
://hi.baidu.com/baidu/ target=_blank title="

论坛徽章:
0
15 [报告]
发表于 2008-08-28 17:53 |只看该作者
经xiaoyu9805119在13楼这么一提示,查了下是放错位置了,低级错误-_-!
re.compile(u'.*http(.*?)>'.encode('gbk'), re.I|re.S).search(body)
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP