字体反爬

451 阅读2分钟

字体反爬

• 字体反爬 是⼀种具有难度的反爬⾍技术,它是通过在⽹站中使⽤特殊字体来替换⽂本内容,使得爬 ⾍⽆法正确解析其中的内容。

• 字体反爬的原理是将⽹站中的⽂本内容转换为特殊的字体格式,然后通过CSS样式来控制字体的显 ⽰⽅式。这种技术的难点在于,爬⾍⽆法直接解析这些特殊格式的字体,导致⽆法获取其中的⽂本 信息。

• 字体反爬可以有效地防⽌爬⾍对⽹站进⾏爬取,从⽽保护⽹站的数据和内容不受到恶意爬取的侵 害。⼤多数影视、房地产、招聘等类型平台习惯采⽤此技术。

fontTools读取字体文件

寻找字体文件: 可以通过源代码搜索@font-face进⾏获取,或者是查找字体⽂件的请求。

import io
from fontTools.ttLib import TTFont
ttf = TTFont(io.BytesIO(requests.get(font_file_uel).content))
# ttf.save('sxs.woff') # 保存为字体⽂件
ttf.saveXML('sxs.xml') # 保存为xml⽂件

字体XML⽂件

image.png

字体关系图

image.png

案例1:(实习僧)

爬取⽹站: www.shixiseng.com

在线查看字体⽂件:1json.com/front/fonte…

在破解字体爬服的时候,数据的获取和原先的爬⾍是⼀样的,先对加密数据的获取。

import requests
import re
import headers as he
from fontTools.ttLib import TTFont
from crawles import unicode_toc


headers,proxies=he.headers()
url='https://www.shixiseng.com/interns?keyword=%E6%96%B0%E5%AA%92%E4%BD%93%E8%BF%90%E8%90%A5&city=%E5%85%A8%E5%9B%BD&type=intern&from=menu'
response=requests.get(url=url,headers=headers).text
# print(response)

#寻找字体所使用的字体文件  @font-face
# @font-face {    font-family: myFont;    src: url(/interns/iconfonts/file?rand=0.648651310533993);}
font_url=re.findall('font-family: myFont;\s+src: url((.*?))',response)
font_url='https://www.shixiseng.com'+font_url[0]
# print(font_url)

# 下载文件
with open('sxs.woff','wb') as file:
    file.write(requests.get(font_url).content)

#读取字体文件
TTFont('sxs.woff').saveXML('sxs.xml')

# 字形编码  &#xe153 &#xe7d8 &#xe7d8-&#xe5d2 &#xe7d8 &#xe7d8
with open('sxs.xml','r')as file:
    cmap = re.findall('<map code="0x(.*?)" name="uni(.*?)"/>',file.read())

cmap_dict={f'&#x{k}':unicode_toc(f'\u{v}') for k,v in cmap}
print(cmap_dict)

# 还原数据
response_text=response
for k,v in cmap_dict.items():
    response_text=response_text.replace(k,v)
print(response_text)

思路分析:

  1. 首先获得源文件,找出加密的字体文件;
  2. 对加密的字体文件进行下载;
  3. 读取下载的字体文件,保存为xml格式;
  4. 通过另保存的xml文件,进行文字编码
  5. 根据文字编码,还原数据

案例2:(次元岛案例)

import requests
import re
import headers as he
from fontTools.ttLib import TTFont
from crawles import unicode_toc

headers,proxies=he.headers()
url='http://ciyuandao.com/photo/list/0-4-1'
response=requests.get(url=url,headers=headers).text
# print(response)
print(unicode_toc(response.replace(';', '').replace('&#x', '\u')))

并不是所有的字体反爬都存放在字体⽂件中,某些⽹站就是直接置换unicode编码进⾏字体反爬。

案例3(土地拍卖)

import requests
import re
import headers as he
from fontTools.ttLib import TTFont
from crawles import unicode_toc

url='https://www.xuanzhi.com/zhaopaigua'
headers,proxies=he.headers()
response=requests.get(url,headers=headers,proxies=proxies).text
# print(response)

#寻找字体所使用的字体文件  @font-face
font_url=re.findall("url('(.*?)') format('woff'), /*",response)[0]
# print(font_url)

# 下载文件
with open('tdpm.woff','wb')as file:
    file.write(requests.get(font_url).content)

#读取字体文件
TTFont('tdpm.woff').saveXML('tdpm.xml')
with open('tdpm.xml','r')as file:
    cmap=re.findall('<map code="0x(.*?)" name="*#(.*?)"/>',file.read())

# 字形编码 
cmap_dict={unicode_toc(f'\u{k}'):v for k,v in cmap}
print(cmap_dict)

# 还原数据
response_text=response
for k,v in cmap_dict.items():
    response_text=response_text.replace(k,v)
print(response_text)