Reputation: 3472
I am parsing a feed url and getting the article content. But, when i try to render the content to html page in HttpResponse, the html elements or tags in the content are rendered as it is, like rest of the content. Do I have to process the content to display the html stuff in it before rendering it to django template?
Also, how to get the favicon for a given feed and render in django template.
I am a newbie to django and python. I have no idea how to do this.
I am using feedparser to parse feed urls.
I am using following code to get the url list from an article content. NOw, how should i get the favicon url because the favicon in some content are rendered as .png format and there are a couple of .png links. how to differentiate which one is favicon?
import feedparser
import lxml.html as lh
import urllib2
#Import Feed for Parsing
d = feedparser.parse("http://www.popgadget.net/atom.xml")
# Print feed name
print d['feed']['title']
# Determine number of posts and set range maximum
posts = len(d['entries'])
# Collect Post URLs
for post in d['entries']:
link=post['link']
print('Parsing {0}'.format(link))
doc=lh.parse(urllib2.urlopen(link))
imgs=doc.xpath('//img[@class="bpImage"]')
for img in imgs:
print(img.attrib['src'])
Upvotes: 1
Views: 1804
Reputation: 33420
I am parsing a feed url and getting the article content. But, when i try to render the content to html page in HttpResponse, the html elements or tags in the content are rendered as it is, like rest of the content. Do I have to process the content to display the html stuff in it before rendering it to django template?
I think that Django is escaping the HTML fetched by feedparser. The reason for this is that the HTML may contain malicious HTML and eventually malicious tags.
If you really want to display the HTML fetched by feedparser without Django escaping, using the |safe filter.
Also, how to get the favicon for a given feed and render in django template
Download the feed, use httplib
Find icon or logo tags, the favicon url should be the contents of the tag, use lxml
Display it using an img tag in your template
Upvotes: 3