要爬取它的数字字体如图
首先要获得其字体文件.ttf
再网页源代码里寻找
下载此ttf文件并在FontCreator里查看
用一个新字典把英文对应成数字和 . 就可以了,然后对应的键值关系进行替换
步骤:
1.用re把.ttf文件下载下来并用fontTools库进行解析,文件是动态加载的
2.找到文件里字体对应的编码(变的)对应于数字字典里的数字
3.进行编码和数字的替换并组合成字符串
要注意的是:
提取字体编码的时候不可以用解析库进行解析,不然编码会被解析成????????????????????了,所以只可用正则表达式把所有编码内容匹配出来
由于是测试解决反爬,此处只爬取一页做测试
import requests
import re
from lxml import etree
from fontTools.ttLib import TTFont
from io import BytesIO
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36',
}
def get_font(url):
response = requests.get(url, headers=headers).content.decode('utf-8')
font_url = re.search("format\('woff'\), url\('(.*?)'\) format\('truetype'\);", response, re.S).group(1)
# print(font_url)
font_content = requests.get(font_url, headers=headers)
font = TTFont(BytesIO(font_content.content))
cmap = font.getBestCmap()# 获得字体加密编码对应解码值的字典
font.close()
# print(cmap)
return cmap
def get_encode(cmap,decoded_string):
word_match_dict = {
'period': '.', 'zero': '0', 'one': '1', 'two': '2', 'three': '3', 'four': '4',
'five': '5', 'six': '6', 'seven': '7', 'eight': '8', 'nine': '9',
}
encode_string = ''
decoded_one_list = decoded_string.split(';')
# 因为原字符串最后一个符号是;这里最后一个元素为空,要除去这个空值,不然会keyError
decoded_one_list.pop(-1)
for decoded_one in decoded_one_list:
decoded_one = int(decoded_one[2:])# 不要&#
key = cmap[decoded_one]
encode_string += word_match_dict[key]
return encode_string
def get_info(url,cmap):
response = requests.get(url, headers=headers).text
'''不可以用解析库解析,因为编码会自动被解析掉,只可通过re'''
# selector = etree.HTML(response)
# p = selector.xpath('//div[@class="book-detail-wrap center990"]/div[1]/div[2]/p[3]')[0]
# x = p.xpath('em[1]/span/text()')[0]
# y = get_encode(cmap,x)
# print(y)用解析库解析后这些编码就变成小正方型了,而得不到加密的编码
content = re.findall('</style><span.*?>(.*?)</span>',response,re.S)
print(content)
for i in content:
decode_content = get_encode(font,i)
print(decode_content,end=' ')
if __name__ == '__main__':
start_url = 'https://book.qidian.com/info/1013552688'
font = get_font(start_url)
get_info(start_url,font)
结果:
参考博客
https://blog.csdn.net/IT_arookie/article/details/83180875
https://blog.csdn.net/qq_35741999/article/details/82018049