分享一个简单的rss阅读工具

Y2J
Lepaskan: 2017-05-03 09:32:10
asal
2158 orang telah melayarinya

#!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("<.*?>",&#39;&#39;,description)
        description=re.sub("\r",&#39;&#39;,description)
        description=re.sub("\n",&#39;&#39;,description)
        description=re.sub(" "," ",description)
        if len(description)>240:
            description=description[:240]+&#39;...&#39;
        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=&#39;&#39;
    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",&#39;entry&#39;]):
        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()
Salin selepas log masuk

Atas ialah kandungan terperinci 分享一个简单的rss阅读工具. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!