#!/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