用Python3提取网页中的超链接

最近有朋友给我指出,我此前写的博文《用Python提取网页中的超链接》(原文地址:http://www.sunbloger.com/article/442.html)中,给出的代码在Python3下运行报错。下面给出在Python3的代码写法:

import urllib.request
import re
 
url = 'http://www.sunbloger.com/'
 
req = urllib.request.urlopen(url)
doc = req.read()
doc = doc.decode('utf-8')
 
links = re.findall(r'href\=\"(http\:\/\/[a-zA-Z0-9\.\/]+)\"', doc)
for a in links:
    print(a)

 

阳光部落原创,更多内容请访问http://www.sunbloger.com/

相关内容:

发表评论