Python 收集RSS网站内容并汇聚为Json格式文件

#!/usr/bin/python 
# -*- coding: utf-8 -*-

'''
Created on 2015-1-24
@author: beyondzhou
@name: harvest_blog_data.py
'''

import os
import json
import feedparser
from html import cleanHtml

FEED_URL = 'http://feeds.feedburner.com/oreilly/radar/atom'

fp = feedparser.parse(FEED_URL)

print "Fetched %s entries from '%s'" % (len(fp.entries[0].title), fp.feed.title)

blog_posts = []
for e in fp.entries:
    blog_posts.append({'title':e.title, 'content':cleanHtml(e.content[0].value),
                            'link':e.links[0].href})

f = open(os.path.join(r"E:", "\\", "eclipse", "Web", "dfile", 'feed.json'), 'w')
f.write(json.dumps(blog_posts, indent=1))
f.close()

print 'Wrote output file to %s' % (f.name, )

# Convert HTML entities back to plain-text representations
def cleanHtml(html):
    from nltk import clean_html
    from BeautifulSoup import BeautifulStoneSoup
    
    if html == "": return ""
    
    return BeautifulStoneSoup(clean_html(html), 
        convertEntities=BeautifulStoneSoup.HTML_ENTITIES).contents[0]

Fetched 33 entries from 'O'Reilly Radar - Insight, analysis, and research about emerging technologies'
Wrote output file to E:\eclipse\Web\dfile\feed.json

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值