练习六十七:HTML练习

题目:一个html文件,找出里面的链接

代码:

from html.parser import HTMLParser
import urllib.request

class myhtml(HTMLParser):
    def __init__(self):
        HTMLParser.__init__(self)
        self.flag = 0
        self.links = []

    def handle_starttag(self,tag,attrs):
        if tag == "a":
            for name,value in attrs:
                if name == "href":
                    self.links.append(value)


if  __name__ == "__main__":
    parser = myhtml()
    myurl = "https://www.cnblogs.com/pinpin"
    html = urllib.request.urlopen(myurl)
    html_connect =html.read()
    html_connect = bytes.decode(html_connect)
    parser.feed(html_connect)
    print(parser.links)
posted @ 2019-02-28 16:24  阳光宝贝-沐沐  阅读(103)  评论(0编辑  收藏  举报