How to get favicon by using beatiful soup and python
Question: Hey guys, I wrote some stupid code for learning just, but it doesn't work for
any sites. here is the code:

    
    
    import urllib2, re
    from BeautifulSoup import BeautifulSoup as Soup
    
    class Founder:
        def Find_all_links(self, url):
            page_source = urllib2.urlopen(url)
            a = page_source.read()
            soup = Soup(a)
    
            a = soup.findAll(href=re.compile(r'/.a\w+'))
            return a
        def Find_shortcut_icon (self, url):
            a = self.Find_all_links(url)
            b = ''
            for i in a:
                strre=re.compile('shortcut icon', re.IGNORECASE)
                m=strre.search(str(i))
                if m:
                    b = i["href"]
            return b
        def Save_icon(self, url):
            url = self.Find_shortcut_icon(url)
            print url
            host = re.search(r'[0-9a-zA-Z]{1,20}\.[a-zA-Z]{2,4}', url).group()
            opener = urllib2.build_opener()
            icon = opener.open(url).read()
            file = open(host+'.ico', "wb")
            file.write(icon)
            file.close()
            print '%s icon succsefully saved' % host
    c = Founder()
    print c.Save_icon('http://lala.ru')
    

The most strange thing is it works for site: <http://habrahabr.ru>
http://5pd.ru

But doesn't work for most others that i've checked. P.S. I know that code is
sucks, please give me some advises maybe.

Thank you


Answer: You're making it far more complicated than it needs to be. Here's a simple way
to do it:

    
    
    import urllib
    page = urllib.urlopen("http://5pd.ru/")
    soup = BeautifulSoup(page)
    icon_link = soup.find("link", rel="shortcut icon")
    icon = urllib.urlopen(icon_link['href'])
    with open("test.ico", "wb") as f:
        f.write(icon.read())
    

