Python3 HTMLParser 获取 Python Events

参考廖雪峰的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)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值