Python項目四:新聞聚合

代碼地址:https://code.csdn.net/ranky2009/pythonsmallproject

編碼中遇到的問題Note:
1.在編寫中發現使用newnews時出現如下錯誤:
502 NEWNEWS command disabled by administrator
 
由於使用newnews命令出現問題,所以改爲使用其他的命令替代。用group 和article命令,見項目代碼。
2.使用如下code打印中文會報錯誤
代碼url_test.py:
from urllib.request import urlopen

with urlopen('http://www.baidu.com') as f:
     print(f.read().decode('utf-8'))
錯誤:
UnicodeEncodeError: 'gbk' codec can't encode character '\ufeff' in position 0: i
llegal multibyte sequence

錯誤環境是OS:WIN7, version of python:3.4.3,窗口:控制檯cmd
後來發現,這個錯誤是由於在控制檯中運行(python url_test.py)代碼。
解決方法:
開始->所有程序->啓動IDLE (Python 3.4 GUI - 64 bit)->File->New File,將以上代碼拷貝到新文件中,F5運行代碼,發現能正確讀取中文。
注意:對於不同的網頁,要看網頁源碼是怎樣編碼方式,有的是utf-8,有的是gb2312。
3.關於match
Match必須從正則表達式開始匹配,
import re
m = re.match(‘.end’, ‘1end’)
print(m.group()) #能夠匹配到1end

n = re.match(‘.end, ‘12end’)
print(n.group()) #n爲None,報錯

 

對書上原始代碼略做修改,如下:

from nntplib import NNTP
from time import strftime, time, localtime
from email import message_from_string
from urllib.request import urlopen
import textwrap
import re

def wrap(string, max = 70):
    return '\n'.join(textwrap.wrap(string)) + '\n'
    
class NewsAgent:
    def __init__(self):
        self.sources = []
        self.destinations = []
        
    def addSource(self, source):
        self.sources.append(source)
        
    def addDestination(self, dest):
        self.destinations.append(dest)
        
    def distribute(self):
        items = []
        for source in self.sources:
            items.extend(source.getItems())
            
        for dest in self.destinations:
            dest.receiveItems(items)
        
            
class NewsItem:
    def __init__(self, title, body):
        self.title = title
        self.body = body

class NNTPSource:
    def __init__(self, servername, group, window):
        self.servername = servername
        self.group = group
        self.window = window
        
    def getItems(self):
        server = NNTP(self.servername)
        groupInfo = server.group(self.group)
        for num in range(5):
            id = str(int(groupInfo[2]) + num)
            articleinfo = server.article(id)[1]
            articleinfoStrings = []
            for line in articleinfo.lines:
                articleinfoStrings.append(line.decode())
            message = message_from_string('\n'.join(articleinfoStrings))
            
            title = message['subject']
            body = message.get_payload()
            
            if message.is_multipart():
                body = body[0]
                
            yield NewsItem(title, body)
            
        server.quit()
        
class SimpleWebSource:
    def __init__(self, url, titlePattern, bodyPattern):
        self.url = url
        self.titlePattern = re.compile(titlePattern)
        self.bodyPattern = re.compile(bodyPattern)
        
    def getItems(self):
        text = urlopen(self.url).read()
        textString = text.decode('utf-8')
        title = self.titlePattern.findall(textString)
        print(title)
        body = self.bodyPattern.findall(textString)
        #print(body[0])
        for line in body:
            if len(line) > 30 :
                yield NewsItem(title[0], wrap(line))
                break
class PlainDestination:
    def receiveItems(self, items):
        for item in items:
            print(item.title)
            print('-' * len(item.title))
            print(item.body)
            
class HTMLDestination:
    def __init__(self, filename):
        self.filename = filename
        
    def receiveItems(self, items):
        out = open(self.filename, 'w', encoding='utf-8')
        print("""
        <html>
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
                <title>Today's New</title>
            </head>
            <body>
            <h1>Today's News</h1>
        """, file = out)
        
        print('<ul>', file = out)
        id = 0
        for item in items:
            id += 1
            print('<li><a href="#%i">%s</a></li>' % (id, item.title), file = out)
        print('</ul', file = out)
        
        id = 0
        for item in items:
            id += 1
            print('<h2><a name="%i">%s</a></h2>' % (id, item.title), file = out)
            print('<pre>%s</pre>' % item.body, file = out)
            
        print("""
            </body>
        </html>
        """, file = out)
        
def runDefaultSetup():
    agent = NewsAgent()
        
    bbc_url = 'http://m.cnr.cn/news/20150706/t20150706_519082828_tt.html?tt_group_id=4658836325'
    bbc_title = '<title>(.+?)</title>'
    bbc_body = '<p.*>(.+?)</p>'
    bbc = SimpleWebSource(bbc_url, bbc_title, bbc_body)
    
    agent.addSource(bbc)

    clpa_server = 'nntpswitch.blueworldhosting.com'
    clpa_group = 'comp.lang.python.announce'
    clpa_window = 1
    clpa = NNTPSource(clpa_server, clpa_group, clpa_window)
        
    agent.addSource(clpa)
        
    agent.addDestination(PlainDestination())
    agent.addDestination(HTMLDestination('news.html'))
        
    agent.distribute()
        
if __name__ == '__main__' : runDefaultSetup()

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章