MIT6_0001F16_ps5
具体题目需查看MIT的python问题pdf文档
代码如下:
# 6.0001/6.00 Problem Set 5 - RSS Feed Filter
# Name:Ding
# Collaborators:
# Time:
import feedparser
import string
import time
import threading
from project_util import translate_html
from mtTkinter import *
from datetime import datetime
import pytz
#-----------------------------------------------------------------------
#======================
# Code for retrieving and parsing
# Google and Yahoo News feeds
# Do not change this code
#======================
def process(url):
"""
Fetches news items from the rss url and parses them.
Returns a list of NewsStory-s.
"""
feed = feedparser.parse(url)
entries = feed.entries
ret = []
for entry in entries:
guid = entry.guid
title = translate_html(entry.title)
link = entry.link
description = translate_html(entry.description)
pubdate = translate_html(entry.published)
try:
pubdate = datetime.strptime(pubdate, "%a, %d %b %Y %H:%M:%S %Z")
pubdate.replace(tzinfo=pytz.timezone("GMT"))
# pubdate = pubdate.astimezone(pytz.timezone('EST'))
# pubdate.replace(tzinfo=None)
except ValueError:
pubdate = datetime.strptime(pubdate, "%a, %d %b %Y %H:%M:%S %z")
newsStory = NewsStory(guid, title, description, link, pubdate)
ret.append(newsStory)
return ret
#======================
# Data structure design
#======================
class NewsStory(object):
#构造函数,根据多个参数完成构造
def __init__(self,guid,title,description,link,pubdate):
self.guid=guid
self.title=title
self.description=description
self.link=link
self.pubdate=pubdate
#一系列简单的类成员函数,旨在安全地访问类成员对象
def get_guid(self):
return self.guid
def get_title(self):
return self.title
def get_description(self):
return self.description
def get_link(self):
return self.link
def get_pubdate(self):
return self.pubdate
#======================
# Triggers
#======================
class Trigger(object):
def evaluate(self, story):
"""
Returns True if an alert should be generated
for the given news item, or False otherwise.
"""
# DO NOT CHANGE THIS!
raise NotImplementedError
# PHRASE TRIGGERS
#对句子小写且删除特殊符号化
def my_change(story):
temp=story[:]
the_valid_list=string.punctuation
for each in the_valid_list:
if each