import re
import requests
from fontTools.ttLib import TTFont
def kuaishou(id):
headers = {
# m端浏览器头,需要cookies但不需要登录,pc两者都要,
# 不论是pc端还是m端,都会偶尔弹验证,手动过一下,cookies还能用
'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Mobile Safari/537.36',
# 带cookie访问
'Cookie': 'did=web_5f471e2368cf4a9980790fe557a624a2'
}
# https://c.kuaishou.com/fw/user/SB810810810
url = 'https://c.kuaishou.com/fw/user/{}'.format(id)
resp = requests.get(url, headers=headers)
content = resp.text
print(content)
# 字体文件处理
ttf_url = re.search('"fontCdnUrl":"(.*?)"', resp.text).group(1).strip()
print(ttf_url)
ttf_resp = requests.get(ttf_url)
try:
with open('ks.ttf', 'wb') as f:
f.write(ttf_resp.content)
except Exception as e:
print(e)
font = TTFont('ks.ttf')
font.saveXML('ks.xml')
uni_list = font.getGlyphOrder()[1:]
first_map = {}
for i, uni in enumerate(uni_list):
if i == 10:
i = '.'
first_map[uni] = i
bestcmap = font['cmap'].getBestCmap()
newmap = dict()
for key, value in bestcmap.items():
key = hex(key)
newmap[key] = value
real_map = {}
for k, v in newmap.items():
for x, y in first_map.items():
if x == v:
key = re.sub('0x', '&#x', k)
real_map[key] = y
for key, value in real_map.items():
if key in content:
content = content.replace(key, str(real_map[key]))
print(content)
fans = re.search("<SPAN STYLE='FONT-FAMILY: kwaiFont;'>(.*?)</SPAN>w?", content, re.S).group(1).strip()
fans = re.sub(';', '', fans)
if '.' in fans:
fans = round(float(fans) * 10000)
else:
fans = int(fans)
print(fans)
if __name__ == '__main__':
kuaishou('SB810810810')