脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python爬取2021猫眼票房字体加密实例

python爬取2021猫眼票房字体加密实例

2021-09-06 00:27宋宋大人 Python

在本篇文章里小编给大家整理的是一篇关于python爬取2021猫眼票房字体加密实例内容,有兴趣的朋友们可以学习下。

春节假期刚过,大家有没有看春节档的电影呢?今年的春节档电影很是火爆,我们可以在猫眼票房app查看有关数据,因为数据一致在更新,所以他的字体是动态的,想要爬取有些困难,再加上猫眼app对字体进行加密,该如何爬取呢?本文介绍反爬2021猫眼票房字体加密的实例。

一、字体加密原理

简单来说就是程序员在设计网站的时候使用了自己设计的字体代码对关键字进行编码,在浏览器加载的时会根据这个字体文件对这些字体进行编码,从而显示出正确的字体。

二、爬取实例

1、得到字体斜率字典

?
1
2
3
4
5
6
7
8
9
10
11
12
13
import requestsimport urllib.request as downimport jsonfrom fontTools.ttLib
import TTFontimport reimport MyPyClass#
得到字体斜率列表(部分)def font_Kdict(mapstype,maps=None):
  '''
  得到字体斜率字典(部分)
  参数:
  mapstype:str->maps类型,判断是是base/new
  maps:映射字典
  return kdict
  kdict字典关系:
  num:Klist 数字对应每条线段的斜率列表
  '''
  kdict={}

2、遍历maps字典,找到对应的num和namecode

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
for num, namecode in maps.items():
   # 跳过无用数据
   if namecode == 'x': continue
   # 判断类型,并从.coordinates得到对应num的所有坐标
   if mapstype=='base':coordinates = namecode.coordinates   
elif mapstype=='new':coordinates=glyf[namecode].coordinates    # 得到坐标 X列表和坐标 Y列表
   x = [i[0] for i in coordinates]
   y = [i[1] for i in coordinates]
   Klist = []
   # 遍历X列表并切片为前10个数据进行斜率计算,即代表绘图的前10条线段的斜率
   for index, absx in enumerate(x[:10]):
     # 当斜率为0/1时,认为斜率为1计算
     if x[index + 1] == x[index] or y[index + 1] == y[index]:
       absxy = 1
     else:
       absxy = (y[index + 1] - y[index]) / (x[index + 1] - x[index])
     # 将斜率加入到列表
     Klist.append(-absxy if absxy < 0 else absxy)
   kdict[num]=Klist    #print('base:', code, Klist, name)
 return kdict

3、对比斜率字典

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
def contrast_K(kbase,knew):
  '''
  对比斜率映射差距
  参数:
  kbase:基础字体映射表的斜率字典
  knew:当前链接的字体映射表的斜率字典
 
  return:dict
  fontMaps:根据对比得出正确的字体映射关系字典
  fontMaps = {}
  # 遍历kbase字典
  for base in kbase.items():
    n = 0 # 成功匹配的斜率个数
    # 遍历knew字典
    for new in knew.items():
      # 遍历kbase>knew>下的两组斜率,进行大小匹配,
      # 如果斜率k的差值小于0.5,并且样本数>=9时,认为两个坐标图形相识只是大小比例不同
      # 即k<=0.5  n>=9
      for (k1,k2) in zip(base[1],new[1]):
        # k取正数
        k=k1-k2 if k1>k2 else k2-k1        if k<=0.5:
          n+=1
          continue
        else:
          break
      if n>=9:
        # 匹配正确则添加进字典中 此时的字典关系是:code:num 代码对应数字的关系
        fontMaps[str(hex(new[0]).replace('0x','&#x'))]=str(base[0])
        break
      n=0
  #print(fontMaps)
  return fontMaps

4、爬取内容

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
with requests.get(url,headers={'user-agent':ua}) as response:
  # 获取存放字典的json字段,并提取字体url
  fontStyle=json.loads(response.content)['fontStyle']
  fontStyle=re.findall('\"([\s\S]*?)\"',fontStyle[::-1])
  fonturl='http:'+fontStyle[0][::-1]# 字体url链接
  # 将加载的字体下载保存到本地,并对其进行分析
  down.urlretrieve(fonturl,'newfont.woff')
  # 爬取的电影数据内容
  content = json.loads(response.content)['movieList']['data']['list']# 信息字典movieNum={}#综合票房数字典movieDayOne= {}#上映首日数量movieRate={}#票房占比movieshowCount={}#排片场次movieViewerAvg={}#场均人数movieInfos={}# 页面内容for i in content:
  moviename=i['movieInfo']['movieName']
  movieNum[moviename]=i['boxSplitUnit']['num']
  movieDayOne[moviename]=i['sumBoxDesc']
  movieRate[moviename]=i['splitBoxRate']
  movieshowCount[moviename]=i['showCount']
  movieViewerAvg[moviename]=i['avgShowView']# 新字体对象fontnew=TTFont('newfont.woff')
# 得到当前字体的映射关系表newNumberMaps=fontnew.getBestCmap()# 获取字形glyf=fontnew['glyf']
# 基础字体斜率字典k_base_dict=font_Kdict(maps=baseNumberMaps,mapstype='base')
# 新字体斜率字典k_new_dict=font_Kdict(maps=fontnew.getBestCmap(),mapstype='new')
# 得到字体映射字典fontcodes=contrast_K(k_base_dict,k_new_dict)# 对加密的字体遍历分组,并去除无用字符
for name,numbercode in movieNum.items():
  movieNum[name]=re.findall('([\S]*?);', numbercode)
# 根据得到的fontcodes映射对加密字体进行替换,得到正确数值for index,(name,numbercodelist)
in enumerate(movieNum.items()):
  num=[]
  # 替换操作
  for code in numbercodelist:
    if '.' in code:
      code=code.replace('.','')
      num.append('.'+fontcodes[code])
    else:
      num.append(fontcodes[code])
  infos=['排行:'+str(index+1),
    '片名',name,
    '上映首日',movieDayOne[name],
    '票房',''.join(num)+'万',
    '票房占比',movieRate[name],
    '场均人数',movieViewerAvg[name]+'人',
    '排片场次',movieshowCount[name]]
  print(infos)

到此这篇关于python爬取2021猫眼票房字体加密实例的文章就介绍到这了,更多相关python爬2021猫眼票房数据内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.py.cn/spider/example/24536.html

延伸 · 阅读

精彩推荐