Chinaunix首页 | 论坛 | 博客
  • 博客访问: 45484
  • 博文数量: 14
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 132
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-07 08:56
个人简介

此博客以后永不更新阴险阴险阴险阴险阴险阴险 国内唯一更新博客在此处不在更新,谢谢,请看下面的地址和邮箱! 我的博客园:http://www.cnblogs.com/mhxy13867806343/ 欢迎访问! 邮箱:mhxy13867806343@sina.cn/ 国内更新博客在:https://github.com/mhxy13867806343 微信公众号:pyjs

文章分类

全部博文(14)

文章存档

2017年(1)

2016年(4)

2015年(8)

2014年(1)

我的朋友

分类: Python/Ruby

2015-09-16 15:27:35

# -*- coding: utf-8 -*-
__author__ = 'Administrator'
:程序的用处有些类似于现在的博客订阅工具或者叫RSS订阅器。
#此题的链接: /> from nntplib import NNTP
import re,time,email,urllib,textwrap
day=24*60*60
def wrap(str1,max1=70):
    return '\n'.join(textwrap.wrap(str1))+'\n'
class NewsAgent:
    def __init__(self):
        self.sources=[]
        self.destinations=[]
    def addSource(self,sou):
        self.sources.append(sou)
    def addDestination(self,dest):
        self.destinations.append(dest)
    def distribute(self):
        items=[]
        for sor in self.sources:
            items.extend(sor.getItems())
        for dest in self.destinations:
            dest.receiveItems(items)

class NewsItem:
    def __init__(self,title,body):
        self.title=title
        self.body=body

class NNPTSOurec:
    def __init__(self,servername,group,windows):
        self.server=servername
        self.group=group
        self.win=windows
    def getItems(self):
        start=time.localtime(time.time()-self.win*day)
        data=time.strftime('%y%m%d',start)
        hour=time.strftime('%H%M%S',start)
        server=NNTP(self.server)
        ids=server.newnews(self.group,data,hour)[1]
        for id1 in ids:
            lines=server.article(id1)[3]
            message=email.message_from_string('\n'.join(lines))
            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.titme1=titlePattern
        self.body1=bodyPattern
    def getItems(self):
        text=urllib.urlopen(self.url).read()
        titles=self.titme1.findall(text)
        bodiles=self.body1.findall(text)
        for title,body in zip(titles,bodiles):
            yield  NewsItem(title,wrap(body))

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')
        print >>out,'''
        <html>
                <head>
                 <title>Today's News</title>
                </head>
                <body>
                <h1>Today's News</hi>
        '''
        print >>out,'<ul>'
        id1=0
        for item in items:
            id1+=1
            print >>out,'<li><a href="#">%s</a></li>'%(id1,item.title)
            print >>out,'</ul>'
            id1=0
            for item in items:
                id1+=1
                print >>out,'<h2><a href=%i>%s</a></h2>'%(id1,item.title)
                print >>out,'<pre>%s</pre>'%(item.body)
                print >>out,'''
                </body>
                </html>
                '''
def runDefaultSetup():
    arent=NewsAgent()
    bbc_url=''
    bbc_title = r'(?s)a href="[^"]*">\s*<b>\s*(.*?)\s*</b>'
    bbc_body = r'(?s)</a>\s*<br/>\s*(.*?)\s*<'
    bbc=SimpleWebSource(bbc_url,bbc_title,bbc_body)
    arent.addSource(bbc)
    clpa_server='news2.neva.ru'
    clpa_group='alt.sex.telephone'
    clpa_windows=1
    clpa=NNPTSOurec(clpa_server,clpa_group,clpa_windows)
    arent.addSource(clpa)
    arent.addDestination(PlainDestination())
    arent.addDestination(HTMLDestination('news_2015.html'))
    arent.distribute()
runDefaultSetup()

我的错误,我的从的代码抄的,别人都对,我这边怎么是错误 的

Traceback (most recent call last):
  File "G:/py_py/python_object_xyz/obj_python/obj_xml.py", line 112, in <module>
    runDefaultSetup()
  File "G:/py_py/python_object_xyz/obj_python/obj_xml.py", line 111, in runDefaultSetup
    arent.distribute()
  File "G:/py_py/python_object_xyz/obj_python/obj_xml.py", line 21, in distribute
    items.extend(sor.getItems())
  File "G:/py_py/python_object_xyz/obj_python/obj_xml.py", line 57, in getItems
    titles=self.titme1.findall(text)
AttributeError: 'str' object has no attribute 'findall'

阅读(1562) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~