记录猫眼电影的自定义字体反爬

猫眼电影用的是动态字体库

猫眼电影榜单国内票房榜,地址:https://maoyan.com/board/1

首先需要获得字体文件,在页面或css里搜@font-face或font-famil

在font刷新页面几次发现字体会变化,字体库是动态的

解决方案

建立字和动态字体库字形的联系

原理

字体用类似表的结构记录字,比如cmap记录了unicode索引和字形,这里反爬用到的表示glyf字形表,表里记录了具体的字形笔画数据,

且表里只记录了字形数据,不关联其他表。有专门的loca表按顺序记录glyf表里字形的位置,在使用字体时通过loca表来找到具体字形。

所以可以利用字形数据来找到自定义字体unicode与字的联系。

字体资料整理记录在: https://www.cnblogs.com/shenyiyangle/p/10700156.html 

找关联思路:

 

1.在猫眼电影下载一个字体做为基准,建立基准字体unicode和字的关系。

2.刷新网页后下载新字体,记为网站字体2,通过比较网站字体1和网站字体2的字形找到unicode和新unicode联系。

3.再通过相同的unicode来建立字和变化字体库unicode的联系,最后将新unicode替换成字。

 代码

headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36"}
r=requests.get("https://maoyan.com/board/1",headers=headers)
font1_url="http:"+re.findall("url\(\'(\/\/.*?woff)\'\)",r.text,re.M)[0]
#创建font目录保存基准字体 if not os.path.exists("font"): font1=requests.get(font1_url,headers=headers) os.mkdir("font") with open("./font/base.woff","wb")as f: f.write(font1.content)

下载一次基准字体并保存到font目录

base_font = TTFont('./font/base.woff')
base_dict=[]
for i in range(len(baseFont.getGlyphOrder()[2:])):
    print(f"对应的数字{i+1}:")
    w=input()
    base_dict.append({"code":baseFont.getGlyphOrder()[2:][i],"num":w})

建立基准字体的unicode和真实字符的关系,查看unicode使用的软件是FontCreator

 

代码只需要执行一次,直接按顺序输入数字

new_font_url="http:"+re.findall("url\(\'(\/\/.*?woff)\'\)",r.text,re.M)[0]
font=requests.get(new_font_url,headers=headers)
with open("new_font.woff","wb")as f:
    f.write(font.content)
new_font = TTFont('new_font.woff')
new_font_code_list=new_font.getGlyphOrder()[2:]

页面改变后的字体下载,获取unicode列表

replace_dic=[]
for i in range(10):
    news = new_font['glyf'][new_font_code_list[i]]
    for j in range(10):
        bases = base_font['glyf'][base_dict[j]["code"]]
        if news == bases: unicode=new_font_code_list[i].lower().replace("uni","&#x")+";" num= base_dict[j]["num"] replace_dic.append({"code":unicode,"num":num})

建立新unicode和字符的关系

org_data=r.text
for i in range(len(replace_dic)):
    new_data=new_data.replace(replace_dic[i]["code"],replace_dic[i]["num"])

全局替换unicode成字符

tree=etree.HTML(org_data)
dds=tree.xpath('//dl[@class="board-wrapper"]/dd')
info=[]
for dd in dds:
    title=dd.xpath('.//p[@class="name"]/a/@title')[0]
    star=dd.xpath('.//p[@class="star"]/text()')[0].replace("主演:","") time=dd.xpath('.//p[@class="releasetime"]/text()')[0].replace("上映时间:","") realticket=dd.xpath('.//p[@class="realtime"]//text()')[1]+dd.xpath('.//p[@class="realtime"]//text()')[2].strip() totalticket=dd.xpath('.//p[@class="total-boxoffice"]//text()')[1]+dd.xpath('.//p[@class="total-boxoffice"]//text()')[2].strip() info.append({"标题":title,"主演":star,"上映时间":time,"实时票房":realticket,"总票房":totalticket})

抓一些信息,下面是结果

保存成csv

import csv
csv_file = open("1325.csv", 'w', newline='')
keys = []
writer = csv.writer(csv_file)
keys = info[1].keys()
writer.writerow(keys)
for dic in info: for key in keys: if key not in dic: dic[key ] = '' writer.writerow(dic.values()) csv_file.close()

结果

用的库

转载于:https://www.cnblogs.com/shenyiyangle/p/10711065.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值