Python笔记 之 使用nntp模块阅读网络新闻

本文代码下载地址

Python代码

登录 news.gmane.io,查看新闻组 gmane.comp.python.committers 前十条信息,每次按下 y 键依次浏览题头和内容。

import nntplib

#默认设置
defaultsite = 'news.gmane.io'
defaultgroup = 'gmane.comp.python.committers'
defaultcount = 10

showhdrs = ['Path','From','Newsgroups','Subject','Date','Lines']

def reader(site=defaultsite,group=defaultgroup,newscount=defaultcount):
    #打印网站相关信息
    print('Connectiong to ',site,' for ',group)
    server = nntplib.NNTP(site)
    print('Version: ',server.nntp_version)
    print('Implementation: ',server.nntp_implementation)
    print(server.getwelcome())
    #获取新闻组信息
    (replay,count,first,last,name) = server.group(group)
    print('%s has %s articles,from %s to %s .'%(name,count,first,last))
    #请求题头
    fecthfrom = str(first)+'-'+str(newscount-1)
    (replay,subjects) = server.xhdr('subject',(fecthfrom))
    print('Reply: ',replay,'\n',
          'Subjects: ',subjects)
    #显示题头,获取消息的题头和主体
    for (id,subj) in subjects:
        print('Article %s [%s]'%(id,subj))
        if  input('Display?') in ('y','Y'):
            #print(server.head(id))
            #hand返回响应信息和文章信息
            #需要两次解读文件内容 lines
            #lines 为byte类型,需要解码
            (replay,articleinfo) = server.head(id)
            (num,id,lines) = articleinfo
            for line in lines:
                line = line.decode('utf-8')
                for prefix in showhdrs:
                    if line[:len(prefix)] == prefix:
                        print(line)
            if input('Show body?') in ('y','Y'):
                #print(server.body(id))
                # body返回响应信息和文章内容
                # 需要两次解读文件内容 lines
                # lines 为byte类型,需要解码
                (replay,articleinfo)=server.body(id)
                (num, id, lines) = articleinfo
                for line in lines:
                    print(line.decode('utf-8'))

    print(server.quit())

if __name__ == '__main__':
    reader()

运行结果

Connectiong to  news.gmane.io  for  gmane.comp.python.committers
Version:  2
Implementation:  INN 2.6.3
200 news.gmane.io InterNetNews NNRP server INN 2.6.3 ready (posting ok)
gmane.comp.python.committers has 7150 articles,from 1 to 7151 .
Reply:  221 Header or metadata information for subject follows (from overview) 
 Subjects:  [('1', 'Nominate Hirokazu Yamamoto (oceancity) for commit privs.'), ('2', 'Re: Nominate Hirokazu Yamamoto (oceancity) for commit privs.'), ('3', 'Re: Nominate Hirokazu Yamamoto (oceancity) for commit privs.'), ('4', 'Re: Nominate Hirokazu Yamamoto (oceancity) for commit privs.'), ('5', 'Re: Nominate Hirokazu Yamamoto (oceancity) for commit privs.'), ('6', 'Re: [Python-Dev] next beta'), ('7', 'Re: [Python-Dev] next beta'), ('8', 'Re: [Python-Dev] next beta'), ('9', 'Re: Nominate Hirokazu Yamamoto (oceancity) for commit privs.')]
Article 1 [Nominate Hirokazu Yamamoto (oceancity) for commit privs.]
Display?y
Path: news.gmane.org!not-for-mail
From: Trent Nelson <tnelson@onresolve.com>
Newsgroups: gmane.comp.python.committers
Subject: Nominate Hirokazu Yamamoto (oceancity) for
Date: Mon, 11 Aug 2008 20:27:36 +0100
Lines: 5
Show body?y

What do people think about making Hirokazu Yamamoto a committer?  I rarely see any mailing list posts from him, but he sure makes up for it in terms of patches submitted to the issue tracker.  As far as I can tell (I've noticed his regular involvement via patches and whatnot for well over a year), he's a pretty switched on guy with a lot of Windows experience -- every time trunk fails to build with vc6/7/8, for example, he's got patches lined up.


        Trent.
Article 2 [Re: Nominate Hirokazu Yamamoto (oceancity) for commit privs.]
Display?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值