python如何选择需要的图标,如何通过使用美丽的汤和python获取图标

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 successfully 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.

解决方案

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())

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值