网站链接 aHR0cHM6Ly96eHRzLnpqendmdy5nb3YuY24vendtaHd3LyMvaG9tZS9pbmRleC9wdWJsaWMvbW9yZQ==
引出问题

今天在做爬虫的时候,遇到了返回的数据是乱码的形式,这不是编码导致的,是字体反扒
同时在数据请求后面,还有woff文件的请求

这里就能确定是字体反扒。
字体反扒解决方案
使用python ttfont 库, ttfont库可以读取woff文件,并当前woff文件里每个字体的unicode和字符名称
将woff文件下载,然后读取一下对应的unicode 和 字符
这里给提供一个在线查看woff文件内容的网站
https://kekee000.github.io/fonteditor/
这里能看到,将那些复杂的字符都有对应的可读的文字对应

然后将数据获取后,一个个替换就能完成数据的解析
转换函数如下:
def get_unicode_ranges(font):
cmap = font['cmap']
unicode_ranges = {}
for table in cmap.tables:
for char_code, char_value in table.cmap.items():
unicode_char = chr(char_code)
unicode_ranges[unicode_char] = char_value.replace("uni","\\u").encode().decode('unicode-escape')
return unicode_ranges
# 加载WOFF文件
font_path = '你的.woff'
font = TTFont(font_path)
# 获取Unicode范围
unicode_ranges = get_unicode_ranges(font)
print("Supported Unicode Characters:", unicode_ranges)
最后再和数据内容进行替换,得到正确的结果


被折叠的 条评论
为什么被折叠?



