Discuss / Python / super是在做啥?

super是在做啥?

Topic source

Rainsho

#1 Created at ... [Delete] [Delete and Lock User]

为了便于理解,在廖老师的例子中每个print里面都加了个序号,然后parse了一遍今天的会议安排。终于一点点的拼出来了。 为什么要从父类继承那块不是很理解,反正注释掉以后会报错。。。

from html.parser import HTMLParser from html.entities import name2codepoint

class MyHTMLParser(HTMLParser):

def __init__(self):
    super(MyHTMLParser, self).__init__()
    self._flag = ''
    self._t=''

def handle_starttag(self, tag, attrs):
    if ('class', 'event-title') in attrs:
        self._flag = 'title'
    elif len(attrs):
        if attrs[0][0]== 'datetime':
            self._flag= 'time'
        elif ('class', 'say-no-more') in attrs:
            self._flag= 'time2'
        elif ('class', 'event-location') in attrs:
            self._flag='location'

def handle_data(self, data):
    if self._flag == 'title':
        print('-'*30)
        print('Title:', data)
        self._flag=''
    if self._flag == 'time':
        self._t = data
        self._flag=''
    if self._flag == 'time2':
        print('Time: %s%s' %(self._t, data))
        self._t,self._flag='',''
    if self._flag == 'location':
        print('Location:', data)
        self._flag=''

with open('t.txt', 'r', encoding='utf-8') as f: html_date=f.read()

parser = MyHTMLParser() parser.feed(html_date)

我也遇到了,从父类继承是干啥?


  • 1

Reply