(转)Python:正则表达式找出网页上所有链接

时间:2014-01-21 17:48:43   收藏:0   阅读:434

转自:http://www.linuxany.com/archives/596.html

bubuko.com,布布扣
import re
import urllib
def test(html,rex):
    alist = []
    r = re.compile(rex)
    matchs = r.findall(html)
    if matchs != None:
        for found in matchs:
            if found not in alist:
                alist.append(found)         
    return alist
             
rex = r<a\s*href=\"(.*?)\"
page=urllib.urlopen(http://hi.baidu.com)
html=page.read()
page.close()
 
print test(html,rex)
bubuko.com,布布扣

原文:http://www.cnblogs.com/youthdream/p/3527787.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!