当前位置:   article > 正文

python 破解字体反爬 (一)_ttf转xml

ttf转xml

爬取一些网站的信息时,偶尔会碰到这样一种情况:网页浏览显示是正常的,用python爬取下来是乱码,F12用开发者模式查看网页源代码也是乱码。这种一般是网站设置了字体反爬

这里我们以58同城为例:

点击进入https://sz.58.com/chuzu/链接:

网页显示数据为:

网页原码数据为: 

 

 从上面可以看出,生这个字变成了乱码,请大家特别注意箭头所指的数字。

在这里里我们确定该网站设置了字体反爬;

一说到字体反爬那我们得先了解CSS3 @font-face 规则:

 

ttf文件: *.ttf是字体文件格式。TTF(TrueTypeFont)是Apple公司和Microsoft公司共同推出的字体文件格式,随着windows的流行,已经变成最常用的一种字体文件表示方式。

@font-face 是CSS3中的一个模块,主要是实现将自定义的Web字体嵌入到指定网页中去。

font-face是css3中允许使用自定义字体的一个模块。

基础知识参考:https://www.runoob.com/cssref/css3-pr-font-face-rule.html

 

了解完这个知识点我们就可以开始下一步操作了:

(1)找到字体文件,并保存下来格式为.ttf:

回到网页上,右键查看网页源代码,搜索font-face关键字,可以看到一大串用base64加密的字符,把这些加密字符复制下来,直接进行解密,但是解密出来的其实是乱码,这个时候其实要做的很简单,把解密后的内容保存为.ttf格式即可。

在python中用base64对复制下来的加密字符进行解码并保存为58.ttf

  1. import base64
  2. font_face='AAEAAAALAIAAAwAwR1NVQiCLJXoAAAE4AAAAVE9TLzL4XQjtAAABjAAAAFZjbWFwq8B/ZwAAAhAAAAIuZ2x5ZuWIN0cAAARYAAADdGhlYWQTmDvfAAAA4AAAADZoaGVhCtADIwAAALwAAAAkaG10eC7qAAAAAAHkAAAALGxvY2ED7gSyAAAEQAAAABhtYXhwARgANgAAARgAAAAgbmFtZTd6VP8AAAfMAAACanBvc3QFRAYqAAAKOAAAAEUAAQAABmb+ZgAABLEAAAAABGgAAQAAAAAAAAAAAAAAAAAAAAsAAQAAAAEAAOu1IchfDzz1AAsIAAAAAADYCHhnAAAAANgIeGcAAP/mBGgGLgAAAAgAAgAAAAAAAAABAAAAC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AhIicTFBYzMjY1NCYjIga5gJTQ5QICZvHD/wABGN/nAQT+sP7Xo3FxoI16pqWHfaTSSgFIAS4CAsIBDNbkASX+lf6l/lP+MjUEHJy3p3en274AAAAAABAAxgABAAAAAAABAA8AAAABAAAAAAACAAcADwABAAAAAAADAA8AFgABAAAAAAAEAA8AJQABAAAAAAAFAAsANAABAAAAAAAGAA8APwABAAAAAAAKACsATgABAAAAAAALABMAeQADAAEECQABAB4AjAADAAEECQACAA4AqgADAAEECQADAB4AuAADAAEECQAEAB4A1gADAAEECQAFABYA9AADAAEECQAGAB4BCgADAAEECQAKAFYBKAADAAEECQALACYBfmZhbmdjaGFuLXNlY3JldFJlZ3VsYXJmYW5nY2hhbi1zZWNyZXRmYW5nY2hhbi1zZWNyZXRWZXJzaW9uIDEuMGZhbmdjaGFuLXNlY3JldEdlbmVyYXRlZCBieSBzdmcydHRmIGZyb20gRm9udGVsbG8gcHJvamVjdC5odHRwOi8vZm9udGVsbG8uY29tAGYAYQBuAGcAYwBoAGEAbgAtAHMAZQBjAHIAZQB0AFIAZQBnAHUAbABhAHIAZgBhAG4AZwBjAGgAYQBuAC0AcwBlAGMAcgBlAHQAZgBhAG4AZwBjAGgAYQBuAC0AcwBlAGMAcgBlAHQAVgBlAHIAcwBpAG8AbgAgADEALgAwAGYAYQBuAGcAYwBoAGEAbgAtAHMAZQBjAHIAZQB0AEcAZQBuAGUAcgBhAHQAZQBkACAAYgB5ACAAcwB2AGcAMgB0AHQAZgAgAGYAcgBvAG0AIABGAG8AbgB0AGUAbABsAG8AIABwAHIAbwBqAGUAYwB0AC4AaAB0AHQAcAA6AC8ALwBmAG8AbgB0AGUAbABsAG8ALgBjAG8AbQAAAAIAAAAAAAAAFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACwECAQMBBAEFAQYBBwEIAQkBCgELAQwAAAAAAAAAAAAAAAAAAAAA'
  3. b = base64.b64decode(font_face)
  4. with open('58.ttf','wb') as f:
  5. f.write(b)

在网上搜索下载并安装字体处理软件FontCreator,用软件打开保存的解码文件58.ttf

现在我们可以得到解决问题的思路了:

1、获取自定义字体和正常字体的映射表,比如:9F92对应的数字是2,9EA3对应的是1。

2、把页面上的自定义字体替换成正常字体,这样就可以正常爬取了。

怎样来获取字体映射表呢?静态的还好,我们用FontCreator工具解析后,直接写死到字典中。但是如果字体映射关系是动态的呢?比如,我们刷新当前页面后,再来查看页面源码:

字体映射关系变了,这样的话,就只能请求一次页面,就获取一次映射关系,用第三方库fontTools来实现。

安装fontTools库,直接pip install fontTools

先来看下ttf文件中有哪些信息,直接打开ttf文件那当然看不了,把它转换成xml文件就可以查看了

  1. from fontTools.ttLib import TTFont
  2. font = TTFont('58.ttf') # 打开本地的ttf文件
  3. font.saveXML('58.xml') # 转换成xml

打开xml文件,可以看到类似html标签的文件结构:

点开GlyphOrder标签,可以看到Id和name

点开glyf标签,看到的是name和一些坐标点,这些座标点就是描绘字体形状的,这里不需要关注这些坐标点。

 

 点开cmap标签,是编码和name的对应关系

从这张图我们可以发现,glyph00001对应的是数字0,glyph00002对应的是数字1,以此类推......glyph00010对应的是数字9,

这是怎么发现的呢:

再看看ttf格式:

这样我们不就知道了:

 glyph00010对应的是数字9了,其他的都是这么来的

 

用代码来获取编码和name的对应关系:

  1. from fontTools.ttLib import TTFont
  2. font = TTFont('58.ttf') #打开本地的ttf文件
  3. bestcmap = font['cmap'].getBestCmap()
  4. print(bestcmap)

输出如下:

输出的是一个字典,key是编码的int型

{38006: 'glyph00006', 38287: 'glyph00008', 39228: 'glyph00001', 39499: 'glyph00005', 40506: 'glyph00010', 40611: 'glyph00002', 40804: 'glyph00007', 40850: 'glyph00003', 40868: 'glyph00004', 40869: 'glyph00009'}

我们把这个字典转一下,变成编码和正常字体的映射关系:

  1. import re
  2. from fontTools.ttLib import TTFont
  3. font = TTFont('58.ttf') #打开本地的ttf文件
  4. bestcmap = font['cmap'].getBestCmap()
  5. newmap = dict()
  6. for key in bestcmap.keys():
  7. value = int(re.search(r'(\d+)', bestcmap[key]).group(1)) - 1
  8. key = hex(key)
  9. newmap[key] = value
  10. print(newmap)

输出:

{'0x9476': 5, '0x958f': 7, '0x993c': 0, '0x9a4b': 4, '0x9e3a': 9, '0x9ea3': 1, '0x9f64': 6, '0x9f92': 2, '0x9fa4': 3, '0x9fa5': 8}

现在就可以把页面上的自定义字体替换成正常字体,再解析了,全部代码如下:

  1. '''
  2. #(第一步,保存字体加密的文件)
  3. import base64
  4. font_face='AAEAAAALAIAAAwAwR1NVQiCLJXoAAAE4AAAAVE9TLzL4XQjtAAABjAAAAFZjbWFwq8J/ZQAAAhAAAAIuZ2x5ZuWIN0cAAARYAAADdGhlYWQXicwFAAAA4AAAADZoaGVhCtADIwAAALwAAAAkaG10eC7qAAAAAAHkAAAALGxvY2ED7gSyAAAEQAAAABhtYXhwARgANgAAARgAAAAgbmFtZTd6VP8AAAfMAAACanBvc3QFRAYqAAAKOAAAAEUAAQAABmb+ZgAABLEAAAAABGgAAQAAAAAAAAAAAAAAAAAAAAsAAQAAAAEAAOPOAYBfDzz1AAsIAAAAAADaAUB6AAAAANoBQHoAAP/mBGgGLgAAAAgAAgAAAAAAAAABAAAACwAqAAMAAAAAAAIAAAAKAAoAAAD/AAAAAAAAAAEAAAAKADAAPgACREZMVAAObGF0bgAaAAQAAAAAAAAAAQAAAAQAAAAAAAAAAQAAAAFsaWdhAAgAAAABAAAAAQAEAAQAAAABAAgAAQAGAAAAAQAAAAEERAGQAAUAAAUTBZkAAAEeBRMFmQAAA9cAZAIQAAACAAUDAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFBmRWQAQJR2n6UGZv5mALgGZgGaAAAAAQAAAAAAAAAAAAAEsQAABLEAAASxAAAEsQAABLEAAASxAAAEsQAABLEAAASxAAAEsQAAAAAABQAAAAMAAAAsAAAABAAAAaYAAQAAAAAAoAADAAEAAAAsAAMACgAAAaYABAB0AAAAFAAQAAMABJR2lY+ZPJpLnjqeo59kn5Kfpf//AACUdpWPmTyaS546nqOfZJ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NlY3JldEdlbmVyYXRlZCBieSBzdmcydHRmIGZyb20gRm9udGVsbG8gcHJvamVjdC5odHRwOi8vZm9udGVsbG8uY29tAGYAYQBuAGcAYwBoAGEAbgAtAHMAZQBjAHIAZQB0AFIAZQBnAHUAbABhAHIAZgBhAG4AZwBjAGgAYQBuAC0AcwBlAGMAcgBlAHQAZgBhAG4AZwBjAGgAYQBuAC0AcwBlAGMAcgBlAHQAVgBlAHIAcwBpAG8AbgAgADEALgAwAGYAYQBuAGcAYwBoAGEAbgAtAHMAZQBjAHIAZQB0AEcAZQBuAGUAcgBhAHQAZQBkACAAYgB5ACAAcwB2AGcAMgB0AHQAZgAgAGYAcgBvAG0AIABGAG8AbgB0AGUAbABsAG8AIABwAHIAbwBqAGUAYwB0AC4AaAB0AHQAcAA6AC8ALwBmAG8AbgB0AGUAbABsAG8ALgBjAG8AbQAAAAIAAAAAAAAAFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACwECAQMBBAEFAQYBBwEIAQkBCgELAQwAAAAAAAAAAAAAAAAAAAAA'
  5. b = base64.b64decode(font_face)
  6. with open('58.ttf','wb') as f:
  7. f.write(b)
  8. #(第二步)ttf转换xml
  9. from fontTools.ttLib import TTFont
  10. font = TTFont('58.ttf') # 打开本地的ttf文件
  11. font.saveXML('58.xml') # 转换成xml
  12. #(第三步)用代码来获取编码和name的对应关系:
  13. from fontTools.ttLib import TTFont
  14. font = TTFont('58.ttf') #打开本地的ttf文件
  15. bestcmap = font['cmap'].getBestCmap()
  16. print(bestcmap)
  17. #(第四步)我们把这个字典转一下,变成编码和正常字体的映射关系:
  18. import re
  19. from fontTools.ttLib import TTFont
  20. font = TTFont('58.ttf') #打开本地的ttf文件
  21. bestcmap = font['cmap'].getBestCmap()
  22. newmap = dict()
  23. for key in bestcmap.keys():
  24. value = int(re.search(r'(\d+)', bestcmap[key]).group(1)) - 1
  25. key = hex(key)
  26. newmap[key] = value
  27. print(newmap)
  28. '''
  29. #完整代码
  30. import requests
  31. import re
  32. import base64
  33. import io
  34. from lxml import etree
  35. from fontTools.ttLib import TTFont
  36. url = r'https://sz.58.com/chuzu/'
  37. headers = {
  38. 'User-Agent':'Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)'
  39. }
  40. response = requests.get(url=url,headers=headers)
  41. # 获取加密字符串
  42. base64_str = re.search("base64,(.*?)'\)",response.text).group(1)
  43. b = base64.b64decode(base64_str)
  44. font = TTFont(io.BytesIO(b))
  45. bestcmap = font['cmap'].getBestCmap()
  46. newmap = dict()
  47. for key in bestcmap.keys():
  48. value = int(re.search(r'(\d+)', bestcmap[key]).group(1)) - 1
  49. key = hex(key)#10十进制转换16进制
  50. newmap[key] = value
  51. # 把页面上自定义字体替换成正常字体
  52. response_ = response.text
  53. for key,value in newmap.items():
  54. key_ = key.replace('0x','&#x') + ';'
  55. if key_ in response_:
  56. response_ = response_.replace(key_,str(value))
  57. # 获取任意一个价钱
  58. rec = etree.HTML(response_)
  59. lis = rec.xpath('string(//div[@class="money"]/b[@class="strongbox"]/text())')
  60. print(lis)

 

参考链接:

https://cuiqingcai.com/6431.html

https://blog.csdn.net/Obgo_6/article/details/101169682

https://www.cnblogs.com/eastonliu/p/9925652.html

 

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号