python腳本爬取字體文件的實(shí)現(xiàn)方法
前言
大家應(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-8
import urllib2,cookielib,sys,re,os,zipfile
import 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-8
import urllib2,cookielib,sys,re,os,zipfile
import shutil
import numpy as np
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)
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 i
for i in os.listdir('./'):
if os.path.isdir(i):
os.rmdir(i)
os.chdir('../')
總體操作跟之前的差不多,跑了幾十分鐘下了4000多的字體。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家學(xué)習(xí)或者使用python能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Python通過DOM和SAX方式解析XML的應(yīng)用實(shí)例分享
這篇文章主要介紹了Python通過DOM和SAX方式解析XML的應(yīng)用實(shí)例分享,針對(duì)這兩種解析方式Python都有相關(guān)的模塊可供使用,需要的朋友可以參考下2015-11-11
Python中淺拷貝的四種實(shí)現(xiàn)方法小結(jié)
本文主要介紹了Python中淺拷貝的四種實(shí)現(xiàn)方法小結(jié),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
Python生成隨機(jī)數(shù)組的方法小結(jié)
這篇文章主要介紹了Python生成隨機(jī)數(shù)組的方法,結(jié)合實(shí)例形式總結(jié)分析了Python使用random模塊生成隨機(jī)數(shù)與數(shù)組操作相關(guān)技巧,需要的朋友可以參考下2017-04-04
使用Python進(jìn)行穩(wěn)定可靠的文件操作詳解
在本文中,主要分享一些如何在Python代碼中改善I/O可靠性的見解,大家參考使用吧2013-12-12
Python MySQL數(shù)據(jù)庫基本操作及項(xiàng)目示例詳解
這篇文章主要介紹了Python連接MySQL數(shù)據(jù)庫后的一些基本操作,并以銀行管理系統(tǒng)項(xiàng)目為例,為大家具體介紹了一下部分功能的實(shí)現(xiàn),文中的示例代碼具有一定的學(xué)習(xí)價(jià)值,感興趣的可以了解一下2021-12-12
TensorFlow卷積神經(jīng)網(wǎng)絡(luò)之使用訓(xùn)練好的模型識(shí)別貓狗圖片
今天小編就為大家分享一篇關(guān)于TensorFlow卷積神經(jīng)網(wǎng)絡(luò)之使用訓(xùn)練好的模型識(shí)別貓狗圖片,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03
Python實(shí)現(xiàn)快速計(jì)算24點(diǎn)游戲的示例代碼
這篇文章主要為大家詳細(xì)介紹了Python如何實(shí)現(xiàn)快速計(jì)算24點(diǎn)游戲并獲取表達(dá)式,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-12-12
python定時(shí)檢查啟動(dòng)某個(gè)exe程序適合檢測(cè)exe是否掛了
定時(shí)檢查啟動(dòng)某個(gè)exe程序這種情況下適合檢測(cè)某個(gè)exe程序是否掛了,感興趣的朋友可以了解下,希望本文對(duì)你有所幫助2013-01-01
Python數(shù)據(jù)分析之NumPy常用函數(shù)使用詳解
本篇將介紹怎樣從文件中載入數(shù)據(jù),以及怎樣使用NumPy的基本數(shù)學(xué)和統(tǒng)計(jì)分析函數(shù)、學(xué)習(xí)讀寫文件的方法,并嘗試函數(shù)式編程和NumPy線性代數(shù)運(yùn)算,來學(xué)習(xí)NumPy的常用函數(shù),需要的可以參考一下2022-05-05

