春節(jié)假期剛過,大家有沒有看春節(jié)檔的電影呢?今年的春節(jié)檔電影很是火爆,我們可以在貓眼票房app查看有關(guān)數(shù)據(jù),因為數(shù)據(jù)一致在更新,所以他的字體是動態(tài)的,想要爬取有些困難,再加上貓眼app對字體進行加密,該如何爬取呢?本文介紹反爬2021貓眼票房字體加密的實例。
一、字體加密原理
簡單來說就是程序員在設(shè)計網(wǎng)站的時候使用了自己設(shè)計的字體代碼對關(guān)鍵字進行編碼,在瀏覽器加載的時會根據(jù)這個字體文件對這些字體進行編碼,從而顯示出正確的字體。
二、爬取實例
1、得到字體斜率字典
import requestsimport urllib.request as downimport jsonfrom fontTools.ttLib
import TTFontimport reimport MyPyClass#
得到字體斜率列表(部分)def font_Kdict(mapstype,maps=None):
'''
得到字體斜率字典(部分)
參數(shù):
mapstype:str->maps類型,判斷是是base/new
maps:映射字典
return kdict
kdict字典關(guān)系:
num:Klist 數(shù)字對應(yīng)每條線段的斜率列表
'''
kdict={}
2、遍歷maps字典,找到對應(yīng)的num和namecode
for num, namecode in maps.items():
# 跳過無用數(shù)據(jù)
if namecode == 'x': continue
# 判斷類型,并從.coordinates得到對應(yīng)num的所有坐標(biāo)
if mapstype=='base':coordinates = namecode.coordinates
elif mapstype=='new':coordinates=glyf[namecode].coordinates # 得到坐標(biāo) X列表和坐標(biāo) Y列表
x = [i[0] for i in coordinates]
y = [i[1] for i in coordinates]
Klist = []
# 遍歷X列表并切片為前10個數(shù)據(jù)進行斜率計算,即代表繪圖的前10條線段的斜率
for index, absx in enumerate(x[:10]):
# 當(dāng)斜率為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、對比斜率字典
def contrast_K(kbase,knew):
'''
對比斜率映射差距
參數(shù):
kbase:基礎(chǔ)字體映射表的斜率字典
knew:當(dāng)前鏈接的字體映射表的斜率字典
return:dict
fontMaps:根據(jù)對比得出正確的字體映射關(guān)系字典
fontMaps = {}
# 遍歷kbase字典
for base in kbase.items():
n = 0 # 成功匹配的斜率個數(shù)
# 遍歷knew字典
for new in knew.items():
# 遍歷kbase>knew>下的兩組斜率,進行大小匹配,
# 如果斜率k的差值小于0.5,并且樣本數(shù)>=9時,認為兩個坐標(biāo)圖形相識只是大小比例不同
# 即k=0.5 n>=9
for (k1,k2) in zip(base[1],new[1]):
# k取正數(shù)
k=k1-k2 if k1>k2 else k2-k1 if k=0.5:
n+=1
continue
else:
break
if n>=9:
# 匹配正確則添加進字典中 此時的字典關(guān)系是:code:num 代碼對應(yīng)數(shù)字的關(guān)系
fontMaps[str(hex(new[0]).replace('0x','#x'))]=str(base[0])
break
n=0
#print(fontMaps)
return fontMaps
4、爬取內(nèi)容
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')
# 爬取的電影數(shù)據(jù)內(nèi)容
content = json.loads(response.content)['movieList']['data']['list']# 信息字典movieNum={}#綜合票房數(shù)字典movieDayOne= {}#上映首日數(shù)量movieRate={}#票房占比movieshowCount={}#排片場次movieViewerAvg={}#場均人數(shù)movieInfos={}# 頁面內(nèi)容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')
# 得到當(dāng)前字體的映射關(guān)系表newNumberMaps=fontnew.getBestCmap()# 獲取字形glyf=fontnew['glyf']
# 基礎(chǔ)字體斜率字典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)
# 根據(jù)得到的fontcodes映射對加密字體進行替換,得到正確數(shù)值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],
'場均人數(shù)',movieViewerAvg[name]+'人',
'排片場次',movieshowCount[name]]
print(infos)
到此這篇關(guān)于python爬取2021貓眼票房字體加密實例的文章就介紹到這了,更多相關(guān)python爬2021貓眼票房數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- Python構(gòu)建圖像分類識別器的方法
- Python爬蟲實例之2021貓眼票房字體加密反爬策略(粗略版)
- 利用python如何實現(xiàn)貓捉老鼠小游戲
- Python貓眼電影最近上映的電影票房信息
- 用Python 爬取貓眼電影數(shù)據(jù)分析《無名之輩》
- python爬蟲開發(fā)之使用Python爬蟲庫requests多線程抓取貓眼電影TOP100實例
- python爬蟲 貓眼電影和電影天堂數(shù)據(jù)csv和mysql存儲過程解析
- Python通過TensorFlow卷積神經(jīng)網(wǎng)絡(luò)實現(xiàn)貓狗識別
- python調(diào)用opencv實現(xiàn)貓臉檢測功能
- Python爬取酷狗MP3音頻的步驟
- python發(fā)qq消息轟炸虐狗好友思路詳解(完整代碼)
- python使用beautifulsoup4爬取酷狗音樂代碼實例
- Java基礎(chǔ)之ClassLoader詳解