Discuss / Python / 小白交作业

小白交作业

Topic source

#作业 '''

import re class PythonEventHtmlParser(HTMLParser):

def __init__(self):
    HTMLParser.__init__(self)
    self.isTime=False
    self.isTitle=False
    self.isLocation=False
    self.isYear=False
    self.pevent={}
    self.pe_list=[]
def handle_starttag(self, tag, attrs):
    if tag=='time':
        self.isTime=True
    elif('class','event-title') in attrs:
        self.isTitle=True
    elif('class','event-location') in attrs:
        self.isLocation=True
    elif('class','say-no-more') in attrs:
        self.isYear=True
def handle_endtag(self, tag):
    if tag=='li' and len(self.pevent)>0:
        self.pe_list.append(self.pevent)
        self.pevent={}
def handle_startendtag(self, tag, attrs):
    pass
def handle_data(self, data):
    if self.isYear:
        self.isYear=False
        if re.match(r'\d{4}',data.strip()):
            self.pevent["Year"]=data
        else:
            self.pevent={}
    elif self.isTime:
        #print("Day:",data)
        self.pevent["Day"]=data
        self.isTime=False
    elif self.isTitle:
        #print("Title:",data)
        self.pevent["Title"]=data
        self.isTitle=False
    elif self.isLocation:
        #print("Location:",data)
        self.pevent["Location"]=data
        self.isLocation=False
def handle_comment(self, data):
    pass
def handle_entityref(self, name):
    pass
def handle_charref(self, name):
    pass

from urllib import request with request.urlopen('https://www.python.org/events/python-events/') as f: data=f.read()

pe_parser=PythonEventHtmlParser() pe_parser.feed(data.decode('utf-8'))

for i,event in enumerate(pe_parser.pe_list): print(f'--------------第{i}条会议信息-------------------') print(f'会议主题:{event["Title"]}') print(f'会议日期:{event["Day"]}') print(f'会议年份:{event["Year"]}') print(f'会议地点:{event["Location"]}') print('') '''


  • 1

Reply