python脚本爬取字体文件的实现方法
作者:Myths 发布时间:2022-09-07 18:20:55
标签: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多的字体。
来源:https://blog.mythsman.com/2016/03/10/1/


猜你喜欢
- 本文为大家分享了python爱心表白的具体代码,供大家参考,具体内容如下import turtleimport time# 画爱心的顶部de
- pandas中的agg函数python中的agg函数通常用于调用groupby()函数之后,对数据做一些聚合操作,包括sum,min,max
- 目录1.数组重塑1.1 一维数组重塑1.2 多维数组重塑2.数组转置1.数组重塑所谓数组重塑就是更改数组的形状。比如将原来3行4列的数组重塑
- Python中的sys模块主要用于程序与解释器的交互,提供一系列函数和变量来处理Python运行环境1、sys.api_version --
- 前言vue.js的UI组件库,在git上有多个项目,我见的使用者比较多的是iView和Element.两个组件库,组件都很丰富。官网的介绍i
- SQL Server查询速度慢的原因有很,常见的有以下几种:1、没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷)2、I/
- 小计: 开发中遇到子组件需要调用兄弟组件中的方法,如下写个小demo记录下心得,如果你有好的方法,请到评论区域指教父组件示例代码:组件功能解
- time()在PHP中是得到一个数字,这个数字表示从1970-01-01到现在共走了多少秒,很奇怪吧 不过这样方便计算, 要找出前一天的时间
- 本文实例为大家分享了Python自动循环扔QQ邮箱漂流瓶的具体代码,供大家参考,具体内容如下Python代码如下:# coding=utf-
- <%@ Page Language="C#" AutoEventWireup="true" C
- 1.漏洞介绍在XHTML 1.0标准下,使用特殊构造的CSS样式,在Internet Explorer 7.0
- 本文主要介绍我在利用Django写文章时,采用的注册方法。首先说一下整体逻辑思路:•处理用户注册数据,•产生token,生成验证URL,•发
- 今天再为大家提供一种方法:不需要安装Excel也可以导入到我们的SQL Server数据库。首先用SQL Server自身的数据转换功能把E
- keras 深度学习框架中get_value函数运行越来越慢,内存消耗越来越大问题问题描述如上图所示,经过时间和内存消耗跟踪测试,发现是ke
- 前言过年了,家家户户都得贴春联,红红火火过大年~春联是天朝传统节日完美衔接了民族文化的产物,以美好的诗词文字表达美好愿望,是 * 有文学形式
- jQuery获取Select选择的Text和Value: $("#select_id").change(function
- 一、logging模块Python中有一个模块logging,可以直接记录日志# 日志级别# CRITICAL 50# ERRO
- PromisePromise能够处理异步程序。回调地狱JS中或node中,都大量的使用了回调函数进行异步操作,而异步操作什么时候返回结果是不
- 前言尝试用python语言写脚本是好的开始,证明我们有了自动化的思想,这对优秀的程序开发人员是很重要的,电子计算机本来就是要减少重复工作的。
- 1. 查找图像中出现的人脸代码示例:#导入face_recognition模块import face_recognition#将j