字体反爬,下载 .woff 文件

找到源代码里面的 .woff 字体文件,下载下来,下面的代码转换为原来的映射

字体文件是二进制的

from fontTools.ttLib import TTFont

def get_cmap(self,font_nums):
    """
    分析字体所映射的值
    :param font_nums: 当前页面获取到的数组
    :return:
    """
    num_str = ''
    # 读取字体
    font = TTFont("font.woff")

    # 生存成xml文件 酿成python可读,举行分析
    font.saveXML('font.xml')

    # 读取映射表
    best_cmap = font['cmap'].getBestCmap()
    best_glpy = font['cmap'].tables[2].ttFont.getReverseGlyphMap()
    temp_cmap = dict()
    for key, value in best_cmap.items():
        temp_cmap[chr(key)] = value

    for ns in font_nums :
        for n in ns :
            num_str += str((best_glpy[temp_cmap[n]] - 2))
        self.all_num += int(num_str)
        num_str = ''

 

 

读取后的方法,可以对照 xml 文件方便查看:

# 读取字体
font = TTFont("font.woff")


# 生存成xml文件 酿成python可读,举行分析
font.saveXML('font.xml')


"""
获得 name 属性,返回列表
<GlyphOrder>
    <GlyphID id="0" name=".notdef"/>
    ...
</GlyphOrder>
"""
font.getGlyphOrder()


"""
获得 code : name ,返回字典,键的值需要用 chr() 转换一下
<map name="cid00019" code="0x30"/>
...
"""
best_cmap = font['cmap'].getBestCmap()


"""
获得 name : id,返回字典
<GlyphOrder>
    <GlyphID id="0" name=".notdef"/>
    ...
</GlyphOrder>
"""
best_glpy = font['cmap'].tables[2].ttFont.getReverseGlyphMap()

 

总结:

爬到网页的字符 :name 

name :code

先获取 <GlyphOrder> 对应的映射,在获取 <cmap> 里面的映射来找对应关系

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值