程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

python爬蟲-33-python字體反爬,網頁看到的和實際下載的不一致(實操)

編輯:Python

我試了幾個網站,都沒有設置字體反爬,比如說之前還設置了該技術的58同城,汽車之家等,不知道因為什麼現在都沒有這個東西了,這個就不是很了解了。雖然無法實操了,但是我們還是可以找一個字體來串一下流程的,往下看。

1、查找自定義字體

基於什麼樣子的現狀需要去查找自定義字體,這個都很清楚了吧,前文已經說的很清楚了。

那麼既然是前端展示,那肯定是渲染到了前端代碼中了,我們查看網頁源代碼即可查到,一般有兩種方式;

1.1、使用​​base64​​的方法進行加載

將字體庫,使用​​base64​​的方法加密之後,由前端進行加載,這個時候我們如何查找呢;

查看網頁源代碼,在裡面搜索​​@font-face​​​屬性,獲取裡面的​​base64​​​代碼,再用​​python​​進行解密,然後保存到本地即可;

1.2、將字體存放到服務器上

前端使用​​@font-face​​​中的​​url​​方法進行加載,我們拿取到後面這個地址,前面再添加個主域名,一般就能直接將這個字體文件做一個下載,這樣子也拿取到了字體庫。

2、解析字體

由於沒有找到需要反爬的網站,這裡就不發起​​get​​請求了,反正這個是最簡單的一個東西,略過即可。

解析成xml文件

我們通過​​get​​​獲取網頁源代碼之後,再通過​​re.search​​​的方法獲取到​​base64​​的代碼,獲取代碼假示如下:

AAEAAAALAIAAAwAwR1NVQiCLJXoAAAE4AAAAVE9TLzL4XQjtAAABjAAAAFZjbWFwq8x/WwAAAhAAAAIuZ2x5ZuWIN0cAAARYAAADdGhlYWQZdDdJAAAA4AAAADZoaGVhCtADIwAAALwAAAAkaG10eC7qAAAAAAHkAAAALGxvY2ED7gSyAAAEQAAAABhtYXhwARgANgAAARgAAAAgbmFtZTd6VP8AAAfMAAACanBvc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/EwB3AAAAAAAAAAAAAAAAAAAAAAAAAAAACwECAQMBBAEFAQYBBwEIAQkBCgELAQwAAAAAAAAAAAAAAAAAAAAA

  • 1.

然後看下我們如何解析呢;

  • 首先我們獲取的是​​base64​​格式的字符串,需要先解密;
  • 然後進行保存成字體庫格式的文件;
  • 然後我們需要使用到​​python​​​中的​​fontTools​​​來解析字體庫,並存儲為​​xml​​文件格式;

這個時候我們就可以讀取了,來看下:

安裝模塊;

pip install fonttools -i https://pypi.tuna.tsinghua.edu.cn/simple

  • 1.

然後書寫代碼如下:

import base64

from fontTools.ttLib import TTFont

font_base64 = "AAEAAAALAIAAAwAwR1NVQiCLJXoAAAE4AAAAVE9TLzL4XQjtAAABjAAAAFZjbWFwq8x/WwAAAhAAAAIuZ2x5ZuWIN0cAAARYAAADdGhlYWQZdDdJAAAA4AAAADZoaGVhCtADIwAAALwAAAAkaG10eC7qAAAAAAHkAAAALGxvY2ED7gSyAAAEQAAAABhtYXhwARgANgAAARgAAAAgbmFtZTd6VP8AAAfMAAACanBvc3QEQwahAAAKOAAAAEUAAQAABmb+ZgAABLEAAAAABGgAAQAAAAAAAAAAAAAAAAAAAAsAAQAAAAEAAOHnKh5fDzz1AAsIAAAAAADa9nYcAAAAANr2dhwAAP/mBGgGLgAAAAgAAgAAAAAAAAABAAAACwAqAAMAAAAAAAIAAAAKAAoAAAD/AAAAAAAAAAEAAAAKADAAPgACREZMVAAObGF0bgAaAAQAAAAAAAAAAQAAAAQAAAAAAAAAAQAAAAFsaWdhAAgAAAABAAAAAQAEAAQAAAABAAgAAQAGAAAAAQAAAAEERAGQAAUAAAUTBZkAAAEeBRMFmQAAA9cAZAIQAAACAAUDAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFBmRWQAQJR2n6UGZv5mALgGZgGaAAAAAQAAAAAAAAAAAAAEsQAABLEAAASxAAAEsQAABLEAAASxAAAEsQAABLEAAASxAAAEsQAAAAAABQAAAAMAAAAsAAAABAAAAaYAAQAAAAAAoAADAA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lZ3VsYXJmYW5nY2hhbi1zZWNyZXRmYW5nY2hhbi1zZWNyZXRWZXJzaW9uIDEuMGZhbmdjaGFuLXNlY3JldEdlbmVyYXRlZCBieSBzdmcydHRmIGZyb20gRm9udGVsbG8gcHJvamVjdC5odHRwOi8vZm9udGVsbG8uY29tAGYAYQBuAGcAYwBoAGEAbgAtAHMAZQBjAHIAZQB0AFIAZQBnAHUAbABhAHIAZgBhAG4AZwBjAGgAYQBuAC0AcwBlAGMAcgBlAHQAZgBhAG4AZwBjAGgAYQBuAC0AcwBlAGMAcgBlAHQAVgBlAHIAcwBpAG8AbgAgADEALgAwAGYAYQBuAGcAYwBoAGEAbgAtAHMAZQBjAHIAZQB0AEcAZQBuAGUAcgBhAHQAZQBkACAAYgB5ACAAcwB2AGcAMgB0AHQAZgAgAGYAcgBvAG0AIABGAG8AbgB0AGUAbABsAG8AIABwAHIAbwBqAGUAYwB0AC4AaAB0AHQAcAA6AC8ALwBmAG8AbgB0AGUAbABsAG8ALgBjAG8AbQAAAAIAAAAAAAD/EwB3AAAAAAAAAAAAAAAAAAAAAAAAAAAACwECAQMBBAEFAQYBBwEIAQkBCgELAQwAAAAAAAAAAAAAAAAAAAAA"

font = base64.b64decode(font_base64)
with open('font_ceshi.ttf', 'wb') as f:
f.write(font)
f.close()

font = TTFont('font_ceshi.ttf')
font.saveXML('font_ceshi.xml')
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

這樣子就生成了一個對應字符集的​​xml​​文件,打開之後可以看到上文介紹的關系的一一對應,如下:

上圖展示的是,字體本身name之間的對應關系;

剩余內容請轉至VX公眾號 “運維家” ,回復 “192” 查看。



------ “運維家” ,回復 “192”  ------

------ “運維家” ,回復 “192”  ------

------ “運維家” ,回復 “192”  ------


linux文件大小,linux雲主機,linuxmib,linux測試網址,linux核心版本,linux下pe系統,linux拷貝硬盤教程;

linux系統怎麼清空 ,linux中百分號,linux中進程Q的狀態,linux查看進程的信息,linuxpam模塊編寫,ps4裝linux系統;

linux增加路由,小型移動電腦linux,如何修改主機名linux,linux系統如何查看掛載點,linux按m顯示文件,linux網卡信息查詢,如何創建用戶組linux。


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved