python easyicon同类型ico图片批量爬取
这是第二篇有关图片爬取的博客。似乎本人对图片情有独钟。这篇博客主要是还是用于记录我的学习记录。同时,我们在编写界面的时候,经常需要从网上下载一些ico图标用于自定义控件,也许不同的程序员有自己的下载方式,这是我的方式,把它记录下来。
easyicon是一个开放的图片网站,网址为http://www.easyicon.net/
在搜索栏输入关键字之后,可以搜到很多图片。在编写界面的时候,我们有时需要许多风格一致的图片。很幸运的是,easyicon的图片组织是基于风格的。以下进入分析过程:
首先,我在搜索栏输入"close"关键字,结果为:
假如我对第三张图感兴趣,则点击它,进入下载界面:
点击ico图标下载,就能进行ico图标下载。很好,这就是我想要的,接下来就来分析如何获取这个图标下载请求。
首先按F12,在开发者窗口中选择Network,接着点击ico图标下载,发现什么也没有。看来用浏览器本身无法获取这个请求的过程。那么使用抓包工具Fiddle。启动Fiddle后,重新点击下载,
第一项是我想要的,点击查看其信息头:
在这里,先注意一下Referer一项,先来说明一下这个url。后面的段的组成是“图标的id-图标的名称_icon.html”
因此在这里,我知道了,若要爬取这张图片,我需要知道它的id以及它的名称。怎么知道呢?在搜索出来的结果中,网页的元素结构中应该可以找到。如下图图标的结构中,src属性的值的最后一项值就是我们想要的id:="1205793.gif"。而alt就是我们所要的名称。那么就简单了,只要先爬取网页的内容,然后从其中用正则表达式筛选出图表的id及alt。之后就可以生成Referer了。而请求的链接,我们可以在Fiddle中我刚才点击的哪一项的信息头中知道链接为:http://download.easyicon.net/ico/图标id/128/
于是可以编写一个接口:
def crawico(imgid,key,path):try:print('start craw one img..')url='http://download.easyicon.net/ico/'+str(imgid[0])+'/128/'print('craw url:'+url)referer='http://www.easyicon.net/'+str(imgid[0])+'-'+urllib.request.quote(key[0])+'_icon.html'print('referer'+referer)cjar=http.cookiejar.CookieJar()opener=urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cjar))headers=[('Accept','text/html,application/xhtml+xml,*/*'),('Accept-Language','zh-CN'),('UA-CPU','AMD64'),('User-Agent','Mozilla/5.0 (Windows NT 6.3; Win64; x64; Trident/7.0; rv:11.0) like Gecko'),('Referer',referer),('Connection','Keep-Alive'),('Host','download.easyicon.net')]opener.addheaders=headersurllib.request.install_opener(opener)print("craw img:"+url+"to"+path)urllib.request.urlretrieve(url,filename=path)except urllib.error.URLError as e:if hasattr(e,"code"):print(e.code)if hasattr(e,"reason"):print(e.reason)except Exception as e:print("exception:"+str(e))
接着,如何批量的爬取同种类型的图片呢?在close的下载页面中,下拉可以看到这样的项:
点击more,来看看跳转出来的页面的链接地址:http://www.easyicon.net/iconsearch/iconset:Control-icons/,这个地址中http://www.easyicon.net/iconsearch/是固定的,而iconset:Control-icons中Control-icons就是类型的名称。那么,我们只要知道了想要下载的图片的类型,那么就可以构造链接,然后爬取同类型的图片。如何知道图片的类型呢?
在close的下载界面中,我们可以注意到如下项:
没错,这就是我想要的。同样,按F12,分析其脚本:
那么,我们就可以构造正则表达式来抓取这个类型名称。
综上所述,那么我有了这样的爬取方案:首先,选取我们需要的图片,进入其下载界面,获取图标所属的类型,构造类型图标的链接。用链接请求到同种类型的图片的网页,接着将网页下的图片一张一张的爬取下来。
如果同种类型的图标有多页怎么办,比如close这个类型的图标的链接为:http://www.easyicon.net/iconsearch/iconset:Control-icons/,这是第一页,则第二页为:
http://www.easyicon.net/iconsearch/iconset:Control-icons/2/。那么现在我们清除规则了。现在直接贴上这个过程的代码:
def gethtml(url):req=urllib.request.Request(url)req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.3; Win64; x64; Trident/7.0; rv:11.0) like Gecko')data=str(urllib.request.urlopen(req).read())return datadef geteasyiconseturl(urlcontent):setid='href="/iconsearch/iconset:(.+?)/">'ids=re.compile(setid).findall(urlcontent)print('iconset:'+str(ids[0]))url='http://www.easyicon.net/iconsearch/iconset:'+ids[0]+'/'return urldef crawHtml(url,path):try:img='<img src=\".+?>'nextp='<div class="pages_all">(.+?)</div>'ll=url+'1/'data=gethtml(ll)ne=re.compile(nextp).findall(data)nex='href="(.+?)">'nee=re.compile(nex).findall(ne[0])t=0x=1while (t-1<len(nee)-1):data=gethtml(ll)print("craw set:"+ll)imgs=re.compile(img).findall(data)print(str(imgs[0]))imgid='/(\d{7}).gif'keyc='alt="(\w+?)"'#path=input("enter the path that the imgs will be save as:")for i in imgs:imgsid=re.compile(imgid).findall(i)key=re.compile(keyc).findall(i)print(key)imgname=path+'/'+str(x)+'.ico'x+=1crawico(imgsid,key,imgname)print('page:'+str(nee[t]))ll='http://www.easyicon.net'+str(nee[t])print('gen set:'+ll)t+=1except urllib.error.URLError as e:if hasattr(e,"code"):print(e.code)if hasattr(e,"reason"):print(e.reason)except Exception as e:print("exception:"+str(e))