国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python腳本爬取字體文件的實(shí)現(xiàn)方法

2019-11-25 16:12:52
字體:
供稿:網(wǎng)友

前言

大家應(yīng)該都有所體會(huì),為了提高驗(yàn)證碼的識(shí)別準(zhǔn)確率,我們當(dāng)然要首先得到足夠多的測(cè)試數(shù)據(jù)。驗(yàn)證碼下載下來容易,但是需要人腦手工識(shí)別著實(shí)讓人受不了,于是我就想了個(gè)折衷的辦法――自己造驗(yàn)證碼。

為了保證多樣性,首先當(dāng)然需要不同的字模了,直接用類似ttf格式的字體文件即可,網(wǎng)上有很多ttf格式的字體包供我們下載。當(dāng)然,我不會(huì)傻到手動(dòng)下載解壓縮,果斷要寫個(gè)爬蟲了。

實(shí)現(xiàn)方法

網(wǎng)站一:fontsquirrel.com

這個(gè)網(wǎng)站的字體可以免費(fèi)下載,但是有很多下載點(diǎn)都是外鏈連接到其他網(wǎng)站的,這部分得忽略掉。

#coding:utf-8import urllib2,cookielib,sys,re,os,zipfileimport numpy as np#網(wǎng)站登陸cj=cookielib.CookieJar()opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))opener.addheaders=[('User-agent','Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36))')]urllib2.install_opener(opener)#搜索可下載連接def search(path): request=urllib2.Request(path) response=urllib2.urlopen(request) html=response.read() html=html.replace('/n',' ')#將所有的回車去掉,因?yàn)檎齽t表達(dá)式是單行匹配。。。。。。 urls=re.findall(r'<a href="(.*?)" rel="external nofollow" >(.*?)</a>',html) for i in urls:  url,inner=i  if not re.findall(r'Download ',inner)==[] and re.findall(r'offsite',inner)==[] and url not in items:   items.append(url)items=[]#保存下載地址for i in xrange(15): host='http://www.fontsquirrel.com/fonts/list/find_fonts/'+str(i*50)+'?filter%5Bdownload%5D=local' search(host)if not os.path.exists('ttf'): os.mkdir('ttf')os.chdir('ttf')def unzip(rawfile,outputdir): if zipfile.is_zipfile(rawfile):  print 'yes'  fz=zipfile.ZipFile(rawfile,'r')  for files in fz.namelist():   print(files) #打印zip歸檔中目錄   fz.extract(files,outputdir)#解壓縮文件 else:  print 'no'for i in items:  print i request=urllib2.Request('http://www.fontsquirrel.com'+i) response=urllib2.urlopen(request) html=response.read() name=i.split('/')[-1]+'.zip' f=open(name,'w') f.write(html) f.close()#文件記得關(guān)閉,否則下面unzip會(huì)出錯(cuò) unzip(name,'./') os.remove(name)os.listdir(os.getcwd())os.chdir('../')files=os.listdir('ttf/')for i in files:#刪除無用文件 if not (i.split('.')[-1]=='ttf' or i.split('.')[-1]=='otf'):  if os.path.isdir(i):   os.removedirs('ttf/'+i)  else:   os.remove('ttf/'+i)print len(os.listdir('ttf/'))

搞到了2000+個(gè)字體,種類也挺多的,蠻好。

網(wǎng)站二:dafont.com

這個(gè)網(wǎng)站的字體花樣比較多,下載起來也比較方便,惡心的是他的文件名的編碼好像有點(diǎn)問題。

#coding:utf-8import urllib2,cookielib,sys,re,os,zipfileimport shutilimport numpy as npcj=cookielib.CookieJar()opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))opener.addheaders=[('User-agent','Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36))')]urllib2.install_opener(opener)items=[]def search(path): request=urllib2.Request(path) response=urllib2.urlopen(request) html=response.read() html=html.replace('/n',' ') urls=re.findall(r'href=/"(http://dl.dafont.com/dl//?f=.*?)/" >',html) items.extend(urls)for i in xrange(117): host='http://www.dafont.com/new.php?page='+str(i+1) search(host) print 'Page'+str(i+1)+'done' items=list(set(items)) print len(items)if not os.path.exists('ttf2'): os.mkdir('ttf2')os.chdir('ttf2')def unzip(rawfile,outputdir): if zipfile.is_zipfile(rawfile):  print 'yes'  fz=zipfile.ZipFile(rawfile,'r')  for files in fz.namelist():   print(files) #打印zip歸檔中目錄   fz.extract(files,outputdir) else:  print 'no'for i in items:  print i request=urllib2.Request(i) response=urllib2.urlopen(request) html=response.read() name=i.split('=')[-1]+'.zip' f=open(name,'w') f.write(html) f.close() unzip(name,'./') os.remove(name)print os.listdir(os.getcwd())for root ,dire,fis in os.walk('./'):#遞歸遍歷文件夾 for i in fis:  if not (i.split('.')[-1]=='ttf' or i.split('.')[-1]=='otf'):   os.remove(root+i)   print ifor i in os.listdir('./'): if os.path.isdir(i):  os.rmdir(i)os.chdir('../')

總體操作跟之前的差不多,跑了幾十分鐘下了4000多的字體。

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家學(xué)習(xí)或者使用python能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)武林網(wǎng)的支持。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 银川市| 平邑县| 罗田县| 收藏| 庆城县| 西藏| 平武县| 沙田区| 扎兰屯市| 乐业县| 惠水县| 乌海市| 哈巴河县| 东乡族自治县| 尉氏县| 慈溪市| 岑溪市| 油尖旺区| 白玉县| 庐江县| 从江县| 泸溪县| 丰镇市| 遂平县| 浪卡子县| 滦南县| 谢通门县| 浦北县| 林西县| 昌宁县| 大邑县| 德保县| 靖宇县| 海盐县| 平定县| 东方市| 确山县| 义马市| 聊城市| 射阳县| 克什克腾旗|