爬取起点的数据信息,其中数字是加密的,需要下载包pip install fonttools
from fontTools.ttLib import TTFont
font.getBestCmap()
import requests
import re
from lxml import etree
from fontTools.ttLib import TTFont
from io import BytesIO
url = 'https://book.qidian.com/info/1012932890'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36',
}
r = requests.get(url=url, headers=headers)
tree = etree.HTML(r.text)
# 先拿到span的class名字
classname = tree.xpath('//div[contains(@class,"book-info")]/p/em/span/@class')[0]
# 拼接得到字体的url
font_url = 'https://qidian.gtimg.com/qd_anti_spider/{}.ttf'.format(classname)
# 得到span的内容
pattern = re.compile(r'<span class="' + classname + '">(.*?)</span>')
ret = pattern.search(r.text)
# 𘟍𘟎𘟉𘟇𘟐
string = ret.group(1)
# 将字体文件下载下来
r_font = requests.get(url=font_url, headers=headers)
# filename = font_url.split('/')[-1]
# with open(filename, 'wb') as fp:
# fp.write(r_font.content)
# 打开这个字体
font = TTFont(BytesIO(r_font.content))
dic = font.getBestCmap()
# 关闭这个字体
font.close()
print(string)
print(dic)
font_dic = {'one': '1', 'two': '2', 'three': '3', 'four': '4', 'five': '5', 'six': '6', 'seven': '7', 'eight': '8', 'nine': '9', 'zero': '0', 'period': '.'}
# 将string按照分号进行切割
lt = string.split(';')[:-1]
number = ''
for code in lt:
code = code.lstrip('&#')
char_code = dic[int(code)]
number_code = font_dic[char_code]
number += number_code
print(number)