python脚本爬取字体文件的实现方法
作者:Myths 时间:2022-09-07 18:20:55
前言
大家应该都有所体会,为了提高验证码的识别准确率,我们当然要首先得到足够多的测试数据。验证码下载下来容易,但是需要人脑手工识别着实让人受不了,于是我就想了个折衷的办法——自己造验证码。
为了保证多样性,首先当然需要不同的字模了,直接用类似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多的字体。
来源:https://blog.mythsman.com/2016/03/10/1/
标签:python,脚本,爬取
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python3.6 + TensorFlow 安装配置图文教程(Windows 64 bit)
2021-07-28 21:15:04
![](https://img.aspxhome.com/file/2023/1/81661_0s.png)
Python 图像处理: 生成二维高斯分布蒙版的实例
2023-01-31 16:29:06
![](https://img.aspxhome.com/file/2023/8/81538_0s.jpg)
asp利用xmlhttp抓取特定网页内容例子
2008-10-10 12:58:00
SQL Server中使用DTS设计器进行数据转移
2009-01-08 16:15:00
Ajax标签导航效果
2013-07-17 02:02:16
Python判断三段线能否构成三角形的代码
2023-01-21 19:44:25
Python中关于面向对象概念的详细讲解
2022-07-31 08:04:40
![](https://img.aspxhome.com/file/2023/1/78471_0s.png)
前端也应关注安全
2009-03-16 17:02:00
合并网页中的多个script引用实现思路及代码
2023-06-29 09:02:19
Python面向对象程序设计之私有变量,私有方法原理与用法分析
2022-04-17 01:37:50
![](https://img.aspxhome.com/file/2023/3/69053_0s.png)
expression为什么性能差?
2009-05-28 19:12:00
CentOS下php使用127.0.0.1不能连接mysql的解决方法
2023-11-15 08:25:52
python 脚本生成随机 字母 + 数字密码功能
2021-11-27 23:37:10
Ethnique公司logo设计过程和思路
2009-09-19 17:04:00
![](https://img.aspxhome.com/file/UploadPic/20099/19/01-79s.jpg)
再谈Python中的字符串与字符编码(推荐)
2023-06-15 23:25:08
![](https://img.aspxhome.com/file/2023/5/81445_0s.png)
Python处理json字符串转化为字典的简单实现
2022-05-15 01:42:58
python lambda函数及三个常用的高阶函数
2022-09-25 16:40:58
PHP程序员玩转Linux系列 使用supervisor实现守护进程
2023-10-12 14:50:21
![](https://img.aspxhome.com/file/2023/4/65814_0s.jpg)
Django中session登录验证操作指南
2023-12-12 03:34:43
python连接字符串的方法小结
2023-12-29 16:24:49