[Django]-Get favicon from a feed content

2👍

You should go to the index page of the website, read and parse the HTML, and then look at the link tag with a rel of "shortcut icon". Failing that, look at /favicon.ico on the server.

2👍

You can get the favicon from HTML document or look for /favicon.ico on the server. Here is the code:

import lxml.html as lh
import urllib2

link = 'http://www.popgadget.net/'
doc = lh.parse(urllib2.urlopen(link))
favicons = doc.xpath('//link[@rel="shortcut icon"]/@href')
if len(favicons) > 0:
    favicon = favicons[0]
else:
    favicon = "%sfavicon.ico" % link
try:
    urllib2.urlopen(favicon)
except urllib2.HTTPError:
    favicon = None
👤Irfan

Leave a comment