1. 程式人生 > 實用技巧 >58同城html字型反爬

58同城html字型反爬

一、獲取html和字型庫

三步驟:獲取字型檔,解析字型檔,替換字型檔符號

知道原理後非常簡單,需要注意每次生成的字型庫順序是不一樣的

import requests
import re
from fontTools.ttLib import TTFont
from lxml import etree
import json
import base64


def get_html():
    headers = {
        "user-agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36
" } res = requests.get("https://sh.58.com/chuzu/", headers=headers) html = res.text print(html) # 正則匹配到字型庫base64字串 res = re.findall("data:application/font-ttf;charset=utf-8;base64,(.*?)'\) format", html) base64_str = res[0] res = base64.b64decode(base64_str) # 儲存為ttf格式檔案
file = open('58.ttf', 'wb') file.write(res) file.close() # 讀取ttf檔案儲存為xml檔案 font = TTFont('58.ttf') font.saveXML('58.xml') return html

二、解析字型庫

def readXML():
    glyph_dict = {'glyph00001': '0', 'glyph00002': '1', 'glyph00003': '2',
                  'glyph00004': '3', 'glyph00005': '4', '
glyph00006': '5', 'glyph00007': '6', 'glyph00008': '7', 'glyph00009': '8', 'glyph00010': '9'} parser = etree.XMLParser(load_dtd=True) # 首先根據dtd得到一個parser(注意dtd檔案要放在和xml檔案相同的目錄) tree = etree.parse("./58.xml", parser) # 用上面得到的parser將xml解析為樹結構 cmap = tree.xpath("//cmap//map") code_dict = {} for m in cmap: values = m.items() code_dict[values[0][1]] = glyph_dict.get(values[1][1]) code_dict = json.loads(json.dumps(code_dict).replace("0x", "&#x").replace('":', ';":')) return code_dict

使用百度字型檔編輯器檢視字型庫:http://fontstore.baidu.com/static/editor/

三、替換html中的字型符號

def replace_secret_code(raw_string, rep_string, rep_dict):
    """替換加密字型"""
    return raw_string.replace(rep_string, rep_dict[rep_string])

def final_run():
    html=get_html()
    code_dict = readXML()
    new_html = None
    for k in code_dict.keys():
        if not new_html:
            new_html = replace_secret_code(html, k, code_dict)
        else:
            new_html = replace_secret_code(new_html, k, code_dict)
    print(new_html)