python脚本爬取字体文件的实现方法

yipeiwu_com4年前Python爬虫

前言

大家应该都有所体会,为了提高验证码的识别准确率,我们当然要首先得到足够多的测试数据。验证码下载下来容易,但是需要人脑手工识别着实让人受不了,于是我就想了个折衷的办法——自己造验证码。

为了保证多样性,首先当然需要不同的字模了,直接用类似ttf格式的字体文件即可,网上有很多ttf格式的字体包供我们下载。当然,我不会傻到手动下载解压缩,果断要写个爬虫了。

实现方法

网站一:fontsquirrel.com

这个网站的字体可以免费下载,但是有很多下载点都是外链连接到其他网站的,这部分得忽略掉。

#coding:utf-8
import urllib2,cookielib,sys,re,os,zipfile
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)
#搜索可下载连接
def search(path):
 request=urllib2.Request(path)
 response=urllib2.urlopen(request)
 html=response.read()
 html=html.replace('\n',' ')#将所有的回车去掉,因为正则表达式是单行匹配。。。。。。
 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()#文件记得关闭,否则下面unzip会出错
 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+个字体,种类也挺多的,蛮好。

网站二:dafont.com

这个网站的字体花样比较多,下载起来也比较方便,恶心的是他的文件名的编码好像有点问题。

#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多的字体。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家学习或者使用python能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对【听图阁-专注于Python设计】的支持。

相关文章

Python 爬虫学习笔记之正则表达式

Python 爬虫学习笔记之正则表达式

正则表达式的使用 想要学习 Python 爬虫 , 首先需要了解一下正则表达式的使用,下面我们就来看看如何使用。 . 的使用这个时候的点就相当于一个占位符,可以匹配任意一个字符,什么意思...

Python 爬虫图片简单实现

Python 爬虫图片简单实现 经常在逛知乎,有时候希望把一些问题的图片集中保存起来。于是就有了这个程序。这是一个非常简单的图片爬虫程序,只能爬取已经刷出来的部分的图片。由于对这一部分内...

python多线程+代理池爬取天天基金网、股票数据过程解析

python多线程+代理池爬取天天基金网、股票数据过程解析

简介 提到爬虫,大部分人都会想到使用Scrapy工具,但是仅仅停留在会使用的阶段。为了增加对爬虫机制的理解,我们可以手动实现多线程的爬虫过程,同时,引入IP代理池进行基本的反爬操作。...

Python爬取商家联系电话以及各种数据的方法

Python爬取商家联系电话以及各种数据的方法

上次学会了爬取图片,这次就想着试试爬取商家的联系电话,当然,这里纯属个人技术学习,爬取过后及时删除,不得用于其它违法用途,一切后果自负。 首先我学习时用的是114黄页数据。 下面四个是用...

python爬虫使用cookie登录详解

python爬虫使用cookie登录详解

前言: 什么是cookie? Cookie,指某些网站为了辨别用户身份、进行session跟踪而储存在用户本地终端上的数据(通常经过加密)。 比如说有些网站需要登录后才能访问某个...