python中正则表达式,匹配href问题请教

lsupper 2011-06-08 10:31:19
我要匹配href中的链接如:

<a href="http://news.baidu.com/ns?cl=2&rn=20&tn=news&" onmousedown="return ForHeader.setWord(this,\'word\');">


我要提取其中的

http://news.baidu.com/ns?cl=2&rn=20&tn=news&


我用:

href=\"(.+?)\"

但是总会匹配到

href="http://news.baidu.com/ns?cl=2&rn=20&tn=news&" onmousedown="return ForHeader.setWord(this,\'word\');"

有什么办法解决?
...全文
689 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
iambic 2011-06-09
  • 打赏
  • 举报
回复
建议不要使用+?,复杂情况(比如有多个+?和+)有时候出现意想不到的结果。
可以试试href="[^"]+"
不过就你这个具体的问题,应该是些低级错误吧。比如打错了,看错了之类的。自己好好检查下,一步步调试(改下正则,改下字符串,测试,反复)。
lsupper 2011-06-09
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 panghuhu250 的回复:]

findall returns the whole matched part, you only want one group (the (.+?) part).
[/Quote]

确实是人品问题,NND,是python版本问题~~ 2.7 就没问题了~~~ 谢谢大家~~~
panghuhu250 2011-06-09
  • 打赏
  • 举报
回复
findall returns the whole matched part, you only want one group (the (.+?) part).
iambic 2011-06-09
  • 打赏
  • 举报
回复
检查下人品吧……
lsupper 2011-06-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 panghuhu250 的回复:]

在我这儿(python2.6.6),结果是对的。

你的python版本?贴出你测试用的完整的代码。
[/Quote]

def findLinks(htmlString):
"""
搜索htmlString中所有的有效链接地址,方便下次继续使用
"""
linkPattern = re.compile("href=\"(.+?)\"")
return linkPattern.findall(htmlString)
lsupper 2011-06-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 panghuhu250 的回复:]

在我这儿(python2.6.6),结果是对的。

你的python版本?贴出你测试用的完整的代码。
[/Quote]
2.5.4
panghuhu250 2011-06-08
  • 打赏
  • 举报
回复
在我这儿(python2.6.6),结果是对的。

你的python版本?贴出你测试用的完整的代码。

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧