参考廖雪峰的Python教程,实现Linux Python3获取 Python Events

#!/usr/bin/env python3
# coding: utf-8

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

url = "https://www.python.org/events/python-events/"
with request.urlopen(url) as f:
    event_data = f.read().decode("utf-8")

class MyHTMLParser(HTMLParser):
    location_flag = False 
    title_flag = False 
    time_flag = False 
    recent_flag = False 
    missed_flag = False
    event = {}
    event_ret = {"recent": [], "missed": []}

    def handle_starttag(self, tag, attrs):
        if tag == "h2" and attrs and "widget-title" in attrs[0]:
            self.recent_flag = True 
        elif tag == "h3" and attrs and "widget-title just-missed" in attrs[0]:
            self.recent_flag = False 
            self.missed_flag = True 

        if self.recent_flag:
            if tag == "h3" and attrs and "event-title" in attrs[0]:
                self.title_flag = True 
            elif tag == "span" and attrs and "event-location" in attrs[0]:
                self.location_flag = True
            elif tag == "time" and attrs and "datetime" in attrs[0]:
                self.time_flag = True  
        elif self.missed_flag:
            if tag == "h3" and attrs and "event-title" in attrs[0]:
                self.title_flag = True 
            elif tag == "span" and attrs and "event-location" in attrs[0]:
                self.location_flag = True
            elif tag == "time" and attrs and "datetime" in attrs[0]:
                self.time_flag = True 

    def handle_endtag(self, tag):
        pass 

    def handle_startendtag(self, tag, attrs):
        pass 

    def handle_data(self, data):
        if self.location_flag:
            self.event["location"] = data 
            if self.recent_flag:
                self.event_ret["recent"].append(self.event)
            elif self.missed_flag:
                self.event_ret["missed"].append(self.event)
            self.event = {}
            self.location_flag = False

        if self.title_flag:
            self.event["title"] = data 
            self.title_flag = False

        if self.time_flag:
            self.event["time"] = data 
            self.time_flag = False

    def handle_comment(self, data):
        pass

    def handle_entityref(self, name):
        pass

    def handle_charref(self, name):
        pass

def python_event(all_event):
    print("%sUpcoming Events%s" %("\033[1;31;40m", "\033[0m"))
    for event in all_event["recent"]:
        print("%s %s%s%s" %(event["time"], "\033[1;34;40m", event["title"], "\033[0m"))
        print("%s" %(event["location"]))

    print("\n%sYou just missed%s" %("\033[1;31;40m", "\033[0m"))
    for event in all_event["missed"]:
        print("%s %s%s%s" %(event["time"], "\033[1;34;40m", event["title"], "\033[0m"))
        print("%s" %(event["location"]))

parser = MyHTMLParser()
parser.feed(event_data)
all_event = parser.event_ret
python_event(all_event)
Logo

更多推荐