亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2017年04月29日 15:44:36   作者:Myths  
這篇文章主要給大家介紹了利用python腳本爬取字體文件的實(shí)現(xiàn)方法,文中分享了爬取兩個(gè)不同網(wǎng)站的示例代碼,相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。

前言

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

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

實(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',' ')#將所有的回車(chē)去掉,因?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:#刪除無(wú)用文件
 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è)字體,種類(lèi)也挺多的,蠻好。

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

這個(gè)網(wǎng)站的字體花樣比較多,下載起來(lái)也比較方便,惡心的是他的文件名的編碼好像有點(diǎn)問(wè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能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

  • pytorch-RNN進(jìn)行回歸曲線(xiàn)預(yù)測(cè)方式

    pytorch-RNN進(jìn)行回歸曲線(xiàn)預(yù)測(cè)方式

    今天小編就為大家分享一篇pytorch-RNN進(jìn)行回歸曲線(xiàn)預(yù)測(cè)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-01-01
  • Python實(shí)現(xiàn)合并兩個(gè)列表的方法分析

    Python實(shí)現(xiàn)合并兩個(gè)列表的方法分析

    這篇文章主要介紹了Python實(shí)現(xiàn)合并兩個(gè)列表的方法,結(jié)合實(shí)例形式對(duì)比分析了常見(jiàn)的Python列表合并操作技巧,需要的朋友可以參考下
    2018-05-05
  • python連接PostgreSQL數(shù)據(jù)庫(kù)的過(guò)程詳解

    python連接PostgreSQL數(shù)據(jù)庫(kù)的過(guò)程詳解

    這篇文章主要介紹了python連接PostgreSQL數(shù)據(jù)庫(kù)的過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • Python打包工具PyInstaller的安裝與pycharm配置支持PyInstaller詳細(xì)方法

    Python打包工具PyInstaller的安裝與pycharm配置支持PyInstaller詳細(xì)方法

    這篇文章主要介紹了Python打包工具PyInstaller的安裝與pycharm配置支持PyInstaller詳細(xì)方法,需要的朋友可以參考下
    2020-02-02
  • 淺談Python中的繼承

    淺談Python中的繼承

    這篇文章主要介紹了Python中繼承的的相關(guān)資料,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • Python使用datetime庫(kù)實(shí)現(xiàn)對(duì)時(shí)間的獲取方法

    Python使用datetime庫(kù)實(shí)現(xiàn)對(duì)時(shí)間的獲取方法

    這篇文章通過(guò)一個(gè)簡(jiǎn)單示例給大家介紹了Python如何使用datetime庫(kù)實(shí)現(xiàn)對(duì)時(shí)間的獲取方法,文章通過(guò)代碼示例給大家介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下
    2023-11-11
  • Django haystack實(shí)現(xiàn)全文搜索代碼示例

    Django haystack實(shí)現(xiàn)全文搜索代碼示例

    這篇文章主要介紹了Django haystack實(shí)現(xiàn)全文搜索代碼示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • python excel使用xlutils類(lèi)庫(kù)實(shí)現(xiàn)追加寫(xiě)功能的方法

    python excel使用xlutils類(lèi)庫(kù)實(shí)現(xiàn)追加寫(xiě)功能的方法

    今天小編就為大家?guī)?lái)一篇python excel使用xlutils類(lèi)庫(kù)實(shí)現(xiàn)追加寫(xiě)功能的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05
  • python中apply函數(shù)詳情

    python中apply函數(shù)詳情

    這篇文章主要介紹了python中apply函數(shù)詳情,該函數(shù)最有用的是第一個(gè)參數(shù),這個(gè)參數(shù)是函數(shù),相當(dāng)于C/C++的函數(shù)指針,更多詳細(xì)內(nèi)容,需要的小伙伴可以參考下面文章內(nèi)容
    2022-01-01
  • python hough變換檢測(cè)直線(xiàn)的實(shí)現(xiàn)方法

    python hough變換檢測(cè)直線(xiàn)的實(shí)現(xiàn)方法

    這篇文章主要介紹了python hough變換檢測(cè)直線(xiàn)的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07

最新評(píng)論