« »
2008-11-27Python

82

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

  1. import re
  2. import urllib
  3. def test(html,rex):
  4.     alist = []
  5.     r = re.compile(rex)
  6.     matchs = r.findall(html)
  7.     if matchs != None:
  8.         for found in matchs:
  9.             if found not in alist:
  10.                 alist.append(found)         
  11.     return alist
  12.              
  13. rex = r'<a\s*href=\"(.*?)\"'
  14. page=urllib.urlopen('http://hi.baidu.com')
  15. html=page.read()
  16. page.close()
  17.  
  18. print test(html,rex)

您还可能感兴趣的内容

日志信息 »

该日志于2008-11-27 05:10由 admin 发表在Python分类下, 你可以发表评论。除了可以将这个日志以保留源地址及作者的情况下引用到你的网站或博客,还可以通过RSS 2.0订阅这个日志的所有评论。

没有评论

发表评论 »


返回顶部