找到源代码里面的 .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> 里面的映射来找对应关系