当前位置:   article > 正文

Python反反爬系列(一)----K近邻算法与CSS动态字体加密_shape反爬

shape反爬

 声明:文章仅源自个人兴趣爱好,不涉及他用,侵权联系删。

网站不好直接给出,给出论坛无法过审,观看破解过程即可。

1.字体反爬

        字体反爬也就是自定义字体加密映射,通过调用自定义的字体文件来渲染网页中的文字,而网页中的文字不再是文字,而是相应的字体编码,通过复制或者简单的采集是无法采集到编码后的文字内容的。

2.查看字体软件font creator 点我下载,也可不下载,借助网页版工具

3.CSS处理前后的字体

我们看到的网页上的数据是正常的

                              

但是当我们打开开发者工具检查字体时 ,金额和票房数据却变成了类似乱码的字符

                     

我们再检查网页源码,发现数据和上面的都不一样,而且每次请求金额还被加密成不同的密文

            

   多次请求,发现返回的字体文件重复概率太低(仔细观察是有的,就是少)

                                                                        

4.解决思路

       了解CSS 的应该会知道(我不知道),CSS 中有一个 @font-face,它允许网页开发者为其网页指定在线字体。原本是用来消除对用户电脑字体的依赖,现在有了新作用——字体反爬。具体的请查看  https://developer.mozilla.org/zh-CN/docs/Web/CSS/@font-face 再观察源码中的数据,像是编码过后的数据。

      仔细观察发现是一些特定span中的数据经过处理,如下图

                 

      所以我们就查找该class名,找到了其字体样式

      

其中的woff就是字体文件,还有其他的,比如ttf字体,woff2,svg等,这里仅有woff ,可在font栏查看

               

将该字体下载过来,在json字体编辑器中打开,https://font.qqe2.com/,可看到字体,多次刷新的话同样的数字还不一样

我们再次拿处理前后的部分数字拿来进行对比:

  1. 最初数字 2 4 0 1 . 3
  2. 加密后     . 
  3. 字体中 $E290 $ED17 $F1A7 $EFBD $EFBD
  4. uniE290 uniED17 uniF1A7 uniEFBD uniEFBD

发现规律了吧,但是我们知道每次数字位置都是动态... 

5.用TTfont把woff文件转化成xml文件

先将字体转化成xml文件。

  1. import requests
  2. from fontTools.ttLib import TTFont
  3. def woff_xml():
  4. url = "https://vfile.meituan.net/colorstone/167b59ea53b59e17be72018703b759c32284.woff"
  5. woff_dir = r"./colorstone/"
  6. file_name = url.split("/")[-1]
  7. xml_name = file_name.replace(file_name.split(".")[-1], "xml")
  8. save_woff = file_name
  9. save_xml = xml_name
  10. resp = requests.get(url=url)
  11. with open(woff_dir+save_woff, "wb") as f:
  12. f.write(resp.content)
  13. f.close()
  14. font = TTFont(woff_dir+save_woff)
  15. font.saveXML(woff_dir+save_xml)

转换成的数据如图:

                                           

                

仔细查看后,确定和我们字体相关的标签:<GlyphOrder></GlyphOrder> 和 <glyf><TTGlyph/></glyf>,其中<GlyphOrder></GlyphOrder>标签中的数据在上图,我们对<TTGlyph>进行查看:

                       

其中有x,y,Xmin,Ymin,Xmax,Ymax等值,很明显是一些坐标点的信息,其实他就是确定字体形状的坐标,不信我们可以画一下:

  1. import matplotlib.pyplot as plt
  2. import re
  3. str = """"
  4. <contour>
  5. 相应内容复制上来
  6. """
  7. x = [int(i) for i in re.findall(r'<pt x="(.*?)" y=', str)]
  8. y = [int(i) for i in re.findall(r'y="(.*?)" on=', str)]
  9. plt.plot(x, y)
  10. plt.show()
'
运行

我们还可以多测试几个XML文件的相同数字的坐标

   

对比          和6稍微有点差异,可Ta就是6。

网上最初的方法到此都是先抓取一份字体,构建基准字体库,来进行编码对比,相同的则为同一个,但显然现在是不行的。

其实借助坐标图,再仔细观察相同数字的坐标,可以看出,相同数字的坐标x,y的差异并不大,根据同一个字体的坐标差异,其实就可以确定这个数字了。因为存在负数,所以通过abs函数取绝对值,伪代码

  1. #对比两个坐标的差异
  2. def compare(AA, BB):
  3. for i in range(5):
  4. if abs(AA[i][0] - BB[i][0]) < 80 and abs(AA[i][1] - BB[i][1]) < 80:
  5. pass
  6. else:
  7. return False
  8. return True
  9. #True则可视为是同一个字

最后发现也是不行的,直到看到一篇博客说可以用K近邻算法,突然恍然大悟,确实很适合这个问题,关于K近邻,参考文章

K-近邻算法算法及其实战

6.Python代码解决:

    6.1.先获取10套字体作为样本

  1. def save_ten_woff(self):
  2. '''
  3. 获取10套基准字体,最初是存到XML中的,后面发现没必要
  4. :return: None
  5. '''
  6. for i in range(0,10):#获取10套字体作为基准字体
  7. time.sleep(1)
  8. res = requests.get(url=self.start_url,headers=self.headers,proxies=self.proxies)
  9. res.encoding = "utf-8"
  10. part_font_url = re.findall(r"url\('(.{,100}?\.woff)",res.text,re.S)
  11. #请求一次获得部分url
  12. if part_font_url:
  13. font_url = "https:" + part_font_url[0]
  14. file_name = str(i+1)+".woff" #字体文件1.woff
  15. save_woff = file_name
  16. resp = requests.get(url=font_url,proxies=self.proxies)
  17. try:
  18. with open(r"./colorstone/" + save_woff, "wb") as f:#将woff文件保存
  19. f.write(resp.content)
  20. f.close()
  21. # font = TTFont(r"./colorstone/" + save_woff)
  22. # font.saveXML(r"./colorstone/base" + str(i+1)+ ".xml") #保存为base1.xml这样的文件名
  23. print("第{}套基准字体保存完毕!".format((i+1)))
  24. except Exception as e:
  25. print(e)
  26. else:
  27. print("第{}次请求失败,请检查网站是否禁止访问等".format((i+1)))

 6.2.提取样本字体中的数字 + 坐标:

  1. def base_font(self):
  2. '''
  3. 获取10套基准字体中数字对应的x,y值
  4. :return: None
  5. '''
  6. # 查看10套基准字体, 获取数字顺序
  7. # base_num1 = [3,8,9,2,0,1,7,5,4,6]
  8. # base_num2 = [3,6,5,2,4,8,9,1,7,0]
  9. # base_num3 = [6,0,4,8,1,9,5,2,3,7]
  10. # base_num4 = [1,8,2,5,7,9,4,6,3,0]
  11. # base_num5 = [0,9,8,6,1,4,7,3,2,5]
  12. # base_num6 = [9,7,5,8,3,4,6,1,2,0]
  13. # base_num7 = [6,5,9,4,0,2,8,3,1,7]
  14. # base_num8 = [6,5,1,0,4,7,8,2,9,3]
  15. # base_num9 = [0,6,9,5,3,8,4,1,2,7]
  16. # base_num10 = [0,6,2,8,5,9,5,3,1,7]
  17. base_num = [[3,8,9,2,0,1,7,5,4,6],[3,6,5,2,4,8,9,1,7,0],[6,0,4,8,1,9,5,2,3,7],[1,8,2,5,7,9,4,6,3,0],
  18. [0,9,8,6,1,4,7,3,2,5],[9,7,5,8,3,4,6,1,2,0],[6,5,9,4,0,2,8,3,1,7],[6,5,1,0,4,7,8,2,9,3],
  19. [0,6,9,5,3,8,4,1,2,7],[0,6,2,8,5,9,5,3,1,7]]
  20. num_coordinate = []
  21. for i in range(0,10):
  22. woff_path = "./colorstone/"+str(i+1)+".woff"
  23. font = TTFont(woff_path)
  24. obj1 = font.getGlyphOrder()[2:] #过滤到前两个不需要的
  25. for j, g in enumerate(obj1):
  26. coors = font['glyf'][g].coordinates
  27. coors = [_ for c in coors for _ in c]
  28. coors.insert(0, base_num[i][j])
  29. num_coordinate.append(coors)
  30. return num_coordinate

6.3. 在函数knn(self)中:

    6.3.1  获取特征值,目标值

  1. num_coordinate = self.base_font()
  2. data = pd.DataFrame(num_coordinate)
  3. data = data.fillna(value=0)
  4. x = data.drop([0],axis=1)
  5. y = data[0]

    6.3.2 进行数据的分割:训练集和测试集

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25)

    6.3.3 调用KNN算法(这里n的参数由网格验证得出,最优参数为1):

  1. knn = KNeighborsClassifier(n_neighbors=1)
  2. knn.fit(x_train, y_train)

 

  6.4.建立映射,将数字和对应的编码建成字典形式:

  1. def get_map(self):
  2. font = TTFont("./colorstone/target.woff")
  3. glyf_order = font.getGlyphOrder()[2:]
  4. info = []
  5. for g in glyf_order:
  6. coors = font['glyf'][g].coordinates
  7. coors = [_ for c in coors for _ in c]
  8. info.append(coors)
  9. print(info)
  10. knn,length = self.knn()
  11. df = pd.DataFrame(info)
  12. data = pd.concat([df, pd.DataFrame(np.zeros(
  13. (df.shape[0], length - df.shape[1])), columns=range(df.shape[1], length))])
  14. data = data.fillna(value=0)
  15. y_predict = knn.predict(data)
  16. num_uni_dict = {}
  17. for i, uni in enumerate(glyf_order):
  18. num_uni_dict[uni.lower().replace('uni', '&#x') + ';'] = str(y_predict[i])
  19. return num_uni_dict

  6.5.采集数据并替换,获取正确数据:

根据网页结构,提取数据:

  1. def get_info(self):
  2. res = requests.get(url=self.start_url, headers=self.headers)
  3. res.encoding = "utf-8"
  4. part_font_url = re.findall(r"url\('(.{,100}?\.woff)", res.text, re.S)
  5. # 请求一次获得部分url
  6. if part_font_url:
  7. font_url = "https:" + part_font_url[0]
  8. resp = requests.get(url=font_url,proxies=self.proxies)
  9. with open(r"./colorstone/target.woff", "wb") as f: # 保存需要分析的字体文件
  10. f.write(resp.content)
  11. f.close()
  12. html = res.text
  13. map_dict = self.get_map()
  14. for uni in map_dict.keys():
  15. html = html.replace(uni, map_dict[uni])
  16. parse_html = etree.HTML(html)
  17. for i in range(0,11):
  18. name = parse_html.xpath('//dd[{}]//p[@class="name"]/a/@title'.format(i))
  19. star = parse_html.xpath('//dd[{}]//p[@class="star"]/text()'.format(i))
  20. releasetime = parse_html.xpath('//dd[{}]//p[@class="releasetime"]/text()'.format(i))
  21. realtime_amount= parse_html.xpath('//dd[{}]//p[@class="realtime"]//text()'.format(i))
  22. total_amount = parse_html.xpath('//dd[{}]//p[@class="total-boxoffice"]//text()'.format(i))
  23. print("".join(name)," ","".join(star)," ","".join(releasetime),"".join(realtime_amount).replace(" ","").replace("\n",""),"".join(total_amount).replace(" ",""))

打印结果

对比原网页

          

数据完全是一样的,此次动态字体反爬到此就结束了。

参考:

https://www.cnblogs.com/shenyiyangle/p/10711065.html

https://cloud.tencent.com/developer/article/1525768

https://cloud.tencent.com/developer/article/1553787

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/998583
推荐阅读
相关标签
  

闽ICP备14008679号