用python正则表达式提取网页的url,python正则表达式,[Python]代码im


[Python]代码

import reimport urlliburl="http://www.baidu.com"s=urllib.urlopen(url).read()ss=s.replace(" ","")urls=re.findall('<a.*?href=.*?<\/a>',ss,re.I) for i in urls:    print ielse:    print '显示完成'不过这样也有一点问题,如果"<a href="  里面有用到javascript,且用到一个  "> " 小于号的话 就会匹配其他错误的URL,大家有什么更好办法不妨告诉我.>>> <ahref="http://www.baidu.com/gaoji/preferences.html"name="tj_setting">搜索设置</a><ahref=";">登录</a><ahref="">注册</a><ahref="http://news.baidu.com">新 闻</a><ahref="http://tieba.baidu.com">贴 吧</a><ahref="http://zhidao.baidu.com">知 道</a><ahref="http://mp3.baidu.com">MP3</a><ahref="http://image.baidu.com">图 片</a><ahref="http://video.baidu.com">视 频</a><ahref="http://map.baidu.com">地 图</a><ahref="#"name="ime_hw">手写</a><ahref="#"name="ime_py">拼音</a><ahref="#"name="ime_cl">关闭</a><ahref="http://baike.baidu.com">百科</a><ahref="http://wenku.baidu.com">文库</a><ahref="http://www.hao123.com">hao123</a><ahref="http://www.baidu.com/more/">更多>></a><aid="seth"onClick="h(this)"href="/"onmousedown="returnns_c({'fm':'behs','tab':'homepage','pos':0})">把百度设为主页</a><aid="setf"href="">把百度设为主页</a><ahref="})">把百度添加到桌面</a><ahref="http://e.baidu.com/?refer=888">加入百度推广</a><ahref="http://top.baidu.com">搜索风云榜</a><ahref="http://home.baidu.com">关于百度</a><ahref="http://ir.baidu.com">AboutBaidu</a><ahref="/duty/">使用百度前必读</a><ahref="http://www.miibeian.gov.cn"target="_blank">京ICP证030173号</a>显示完成

评论关闭