from html.parser import HTMLParser
from urllib import request
class MyHTMLParser(HTMLParser):
def handle_starttag(self, tag, attrs):
if len(attrs)==1:
if 'event-title' in attrs[0] :
self.pd = '会议名称'
if 'datetime' in attrs[0] :
self.pd = '日期'
if 'event-location' in attrs[0] :
self.pd = '地点'
def handle_data(self, data):
if not self.pd == 0:
print(self.pd+': '+data)
self.pd = 0
逆流6337168474
from html.parser import HTMLParser from urllib import request
class MyHTMLParser(HTMLParser): def handle_starttag(self, tag, attrs): if len(attrs)==1: if 'event-title' in attrs[0] : self.pd = '会议名称' if 'datetime' in attrs[0] : self.pd = '日期' if 'event-location' in attrs[0] : self.pd = '地点'
parser = MyHTMLParser() parser.pd = 0 url = 'https://www.python.org/events/python-events/' with request.urlopen(url) as f: html = f.read() parser.feed(html.decode('utf-8'))