侯体宗的博客
  • 首页
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

Python  /  管理员 发布于 7年前   285

前言

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

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

实现方法

网站一:fontsquirrel.com

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

#coding:utf-8import urllib2,cookielib,sys,re,os,zipfileimport 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="https:/article/(.*?)" 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-8import urllib2,cookielib,sys,re,os,zipfileimport shutilimport numpy as npcj=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 ifor i in os.listdir('./'): if os.path.isdir(i):  os.rmdir(i)os.chdir('../')

总体操作跟之前的差不多,跑了几十分钟下了4000多的字体。

总结

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


  • 上一条:
    Python 操作MySQL详解及实例
    下一条:
    Python在图片中添加文字的两种方法
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 在go语言中实现字符串可逆性压缩及解压缩功能(0个评论)
    • 使用go + gin + jwt + qrcode实现网站生成登录二维码在app中扫码登录功能(0个评论)
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客