快手字体反爬

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')

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值