本帖最后由 ermao 于 2022-7-26 08:47 编辑
功能已失效,原内容已删除,仅保留字体反爬部分,思路还可以供人参考,勿回复
字体反爬部分代码
# -*- encoding: utf-8 -*-
'''
@ File : kuaishou.py
@ Time : 2020年09月27日 15:08:24 星期天
@ Author : ermao
@ Version : 1.0
@ Link : https://erma0.gitee.io
@ Desc : 快手字体反爬
'''
print('start kuaishou.py')
import requests
import re
import time
import os
import json
# import hashlib
# from hashlib import md5
from io import BytesIO
from fontTools.ttLib import TTFont
def get_timestamp(long=13):
if long == 10:
timestamp = str(int(time.time()))
else: # long == 13:
timestamp = str(int(time.time() * 1000))
return timestamp
def user_info(id):
u = {'kuaiId': id}
url = 'https://c.kuaishou.com/fw/user/' + id
try:
res = requests.get(url, headers=headers).text
user_json = htmlRe.search(res).group(1)
userinfo = json.loads(user_json)
u['userId'] = userinfo['userIdInfo']['userId']
u['userName'] = userinfo['share']['title'][4:-12]
u['desc'] = userinfo['share']['desc']
u['imgUrl'] = userinfo['share']['imgUrl']
u['count'] = userinfo['pageTabs'][0]['count']
fontCdnUrl = userinfo['obfuseData']['fontCdnUrl']
# u['fontCdnUrl'] = userinfo['obfuseData']['fontCdnUrl']
# 映射表 网页字符:明文
key_map = {}
# 动态获取字体
font_content = requests.get(fontCdnUrl).content
font = TTFont(BytesIO(font_content)) # 从内存加载
code = font.getGlyphOrder()[1:] # 固定顺序的字符编码列表(去除第一个无效字符)
nums = [ # 固定顺序明文列表
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', 'w', 'k',
'm', '+'
]
temp = dict(zip(code, nums)) # 临时映射(固定顺序),字符编码uni****:明文
res = font.getBestCmap() # 获取关键映射,网页字符转码:字符编码uni****
for k, v in res.items():
# 网页字符去除前缀,再从16进制转10进制,就得到k,这里逆向写,即k转16进制后拼接前缀
kk = '' + str(hex(k))[1:]
# 从临时映射中根据字符编码uni****取出明文
key_map[kk] = temp[v]
# print(len(key_map), key_map)
re_html_code = re.compile(r'[\da-f]{4}')
# for word in ['fan', 'follow', 'photo', 'collect']: # 遍历需要用到的字体加密的参数
for word in ['fan', 'follow']: # 遍历需要用到的字体加密的参数
# ('photo', 'collect'这两个重复了,可以不要)
text = userinfo['obfuseData'][word]
words = re_html_code.findall(text)
u[word] = ''.join(key_map[i] for i in words)
return u
except Exception:
return {'error': '出错!'}
|