1首先我们需要查看网页信息
2查看网页代码发现字体加密
3找到字体加密文件的链接请求链接获取到woff文件
4直接上代码解释
请求模块
import requests
字体读取模块
from fontTools.ttLib import TTFont
创建电影类
class My():
def __init__(self):
self.woff_url = 'https://vfile.meituan.net/colorstone/f0a30a4dda64b4f8f344858115f54fc92296.woff'
self.start_url = 'https://maoyan.com/films/243'
#生成器:data
self.data = {}
self.headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36',
'Cookie': '__mta=88955684.1616235221204.1616248066371.1616251740188.12; uuid_n_v=v1; uuid=F0E49820896411EB83DB97FC29450BC10B658A25C8AA46C381F94D2BFC773E6F; _csrf=a6f5c7fa04c14bccdb80e72023297635e9aad88578cdba6ece9c24f546b08c0e; Hm_lvt_703e94591e87be68cc8da0da7cbd0be2=1616235221; _lx_utm=utm_source=Baidu&utm_medium=organic; _lxsdk_cuid=1784f204fb8c8-0e1d91e1cca1da-5771031-144000-1784f204fb8c8; _lxsdk=F0E49820896411EB83DB97FC29450BC10B658A25C8AA46C381F94D2BFC773E6F; __mta=88955684.1616235221204.1616235223615.1616239389900.4; Hm_lpvt_703e94591e87be68cc8da0da7cbd0be2=1616251740; _lxsdk_s=178501c5de9-e26-cd6-bca||3'
}
页面内容获取函数
def page_content(self):
response = requests.get(url=self.start_url, headers=self.headers)
content = response.text
return content
# 设置主函数
def main(self):
# 1找到css链接,获取里面woff文件的链接
woff文件获取
# response = requests.get(self.woff_url)
# with open('f0a30a4dda64b4f8f344858115f54fc92296.woff', 'wb') as f:
# # woff文件需要二进制文件
# f.write(response.content)
# 2woff请求下载下来。通过TTFont读取woff拿出信息
woff_font = TTFont(r'f0a30a4dda64b4f8f344858115f54fc92296.woff')
# 获取对照表的16进制
keys = woff_font.getGlyphOrder()
# 手动生成一个汉字对照list
word_cn_list = list('6379180425')
# 便利woff文件数据,获取他的所有的16进制数
for i in range(len(keys[2:])):
if keys[i + 2] not in self.data:
self.data[keys[i + 2]] = word_cn_list[i]
# data={'Key(16进制数)': Value(所有代表文字)
# 3替换。将response.text里面所有[& # x+16进制数]
content = str(self.page_content())
for k, v in self.data.items():
主要细节页面中的文件是小写的所以需要设用.lower()方法将其转换成小写进行替换
content = content.replace('&#x' + k[3:].lower() + ';', v)
print(content)
# 4按照正常的方式去获取数据了
if __name__ == '__main__':
my = My()
my.main()