当前位置:首页 / 网页制作 / XML和XSLT教程 / 分享一个简单的rss阅读工具
分享一个简单的rss阅读工具
芯晴素材特效 分类:XML和XSLT教程 发布日期:2018-05-08
#!usr/bin/env python
# -*- coding:UTF-8 -*-
  
import re
from lxml import etree
from bs4 import BeautifulSoup as sp
import requests
import urllib2
import StringIO
  
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
  
headers={'User-Agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'}
  
def urlread(url):
    try:
        req=requests.get(url,headers=headers)
        req.encoding="utf-8"
        return req.text.encode("utf-8")
    except:
        req=urllib2.Request(url,headers=headers)
        response=urllib2.urlopen(req)
        return response.read().encode("utf-8")
              
class Item:
    def __init__(self,title,link,date,description):
        self.title=title.strip()
        self.link=link.strip()
        self.pubDate=date.strip()
        self.decription=self.filter(description).strip()
          
    def filter(self,description):
        description=re.sub("<.*?>",'',description)
        description=re.sub("\r",'',description)
        description=re.sub("\n",'',description)
        description=re.sub("&nbsp;"," ",description)
        if len(description)>240:
            description=description[:240]+'...'
        return description      
  
    def __str__(self):
        return "%s\n%s\n%s\n<%s>\n" % (
                self.title,
                self.link,
                self.decription,
                self.pubDate
                )
          
    __repr__=__str__
      
class BSParser(object):
    #url=''
    def __init__(self,url):
        xml=urlread(url)
        self.reset(xml)
          
    def reset(self,xml=None):
        if xml==None:
            self.soup=sp("<xml> </xml>")
        else:
            self.soup=sp(xml,"xml")
  
    def callback(self,method,obj,tags):
        rst=None
        attr=method.lower()
  
        for tag in tags:
            try:
                rst=getattr(obj,attr)(tag)
            except:
                continue
            if rst:
                break
        return rst
  
    def getfields(self,tags=["item",'entry']):
        return self.callback(method="FIND_ALL",
                            obj=self.soup,
                            tags=tags)
              
    def gettitle(self,obj,tags=["title"]):
        return self.callback("FIND",obj,tags).text
          
    def getlink(self,obj,tags=["link"]):    
        rst=self.callback("FIND",obj,tags).text
        if not rst:         
            rst=self.callback("FIND",obj,tags).get("href")
        return rst
          
    def getdate(self,obj,tags=["pubDate","published"]):
        return self.callback("FIND",obj,tags).text
          
    def getdescription(self,obj,tags=["description","content"]):      
        return self.callback("FIND",obj,tags).text
      
    def run(self):
        for item in self.getfields():
            title=self.gettitle(item)
            link=self.getlink(item)
            date=self.getdate(item)
            description=self.getdescription(item)
            newsitem=Item(title,link,date,description)
            yield newsitem
  
def test():
    parser=Parser()
    for item in parser.run():
        print item
          
if __name__=="__main__":
    test()

版权信息:本站所有资源仅供学习与参考,请勿用于商业用途,如有侵犯您的版权,请及时联系821794221#qq.com(#换@),我们将尽快处理。

您可能在找这些

  • 内容标签:

热门素材

一款CSS+JS打造的个性菜单

2013-01-04   浏览:7845

文字切换插件Adjector.js

2017-03-13   浏览:195

JS+CSS打造的鼠标点击触发的导航菜单

2013-01-04   浏览:9863

html5手机端图片上传裁剪网页下载

2017-03-13   浏览:219

全屏游动的图片,可以飘到浏览器外边

2012-12-30   浏览:7987

推荐一款jQuery版淡出淡入的图片幻灯切

2013-01-15   浏览:8809

jQuery标签插件flyLabel

2017-03-16   浏览:368

JS实现的螺旋效果

2012-12-27   浏览:7912

Canvas粒子线条连接动态背景特效

2017-05-04   浏览:859

用CSS控制滚动条样式

2012-12-31   浏览:8114

又一款纯CSS实现的滑动门,代码非常简

2013-01-04   浏览:9039

前端模拟微信app支付页面效果

2017-03-09   浏览:265