代码:
import re import requests import time from fontTools.ttLib import TTFont from fake_useragent import UserAgent headers = {"UserAgent": UserAgent().random} # 请求的目标网址 url = "https://book.qidian.com/info/1019275790" # 延时等待一秒 time.sleep(1) # 请求目标网页 html = requests.get(url, headers=headers).content.decode() # 保存网页 with open('网页代码.html', 'w', encoding='utf-8') as f: f.write(html) # 传值 html_content = html # 匹配网页中的字体文件路径 font_url = re.findall(r"src: url('(.*?)')", html)[1] print(font_url) # 请求字体文件地址 font_resp = requests.get(font_url) # 保存字体文件 with open('字体文件.woff', 'wb') as f: f.write(font_resp.content) # TTFont打开字体文件 font = TTFont("字体文件.woff") # 将字体文件保存为可读的xml文件 font.saveXML('font.xml') # 找字体的映射关系 font_map = font['cmap'].getBestCmap() print(font_map) # 找到字体映射关系,并手动修改后的字体映射 d = {'three': 3, 'eight': 8, 'four': 4, 'two': 2, 'zero': 0, 'seven': 7, 'six': 6, 'five': 5, 'one': 1, 'period': '.', 'nine': 9} # 将字体的映射关系重新修改 for key in font_map: font_map[key] = d[font_map[key]] print(font_map) # 将网页代码中的加密字体替换为数字 for key in font_map: html_content = html_content.replace('&#' + str(key) + ';', str(font_map[key])) # 保存修改后的网页代码 with open('after.html', 'w', encoding='utf-8') as f: f.write(html_content)
原文链接:https://blog.csdn.net/qq_46292926/article/details/105326362