JS的字体反爬

目标网站闪职数据

方法步骤:

1 、找到字体文件 ;
2 、找到替换关系 ;
3 、替换爬取下来的数据 处理得到正常的数据。

 如何找到字体文件:

  1. 定位到进行了字体反爬的位置 在对应的styles里面找到font‐family
  2. 复制font‐family里面的值 去网页源码里面搜索
  3. 在搜索结果的附近 找到 xxx.ttf 这样的url 进行下载
  4. 如果需要通过python去读取识别字体文件里面的内容: pip install fontTools ‐i  https://pypi.tuna.tsinghua.edu.cn/simple

 找到对应关系:

 

对应关系:

 

 源代码:

闪职.py

import requests
from lxml import etree
from tool import encrypto
from fontTools.ttLib import TTFont

header = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36',
    'Cookie': 'shanzhi_kmer=h9wm0ptr9kcuza527as3a43a6zuwzsid; csrftoken=yHdq0AaPEO1pyiC2zji4MmeyLRNcXVcZLNoHCT3izQ52lwvASvHv0jgsGG9kEXUN'
}

def get_data():
    # 目标url
    url = 'http://shanzhi.spbeen.com/login/'
    # 请求头

    res = requests.get(url,headers=header)
    data = res.text
    return data


def deal_data(text):
    tree = etree.HTML(text)

    # 获取csrfmiddlewaretoken
    csrfmiddlewaretoken = tree.xpath('//input[@name="csrfmiddlewaretoken"]/@value')[0]

    # 取pk
    pk = tree.xpath('//input[@id = "pk"]/@value')[0]

    return csrfmiddlewaretoken,pk

def rebuilt(pk,csrfmiddlewaretoken):

    # 老密码
    old_pwd = 'logic_00'
    # 加密后的密码
    new_pwd = encrypto(pk,old_pwd)

    # 构造表单数据
    data_dict = {
        "username":'logic_00',
        "password": new_pwd,
        "csrfmiddlewaretoken":csrfmiddlewaretoken
    }


    url = 'http://shanzhi.spbeen.com/login/'
    res = requests.post(url,data=data_dict,headers=header)
    return res.text

def deal_ttf(html):
    sz = TTFont('szec.ttf')

    re_dict = sz.getBestCmap()
    # discover what the old dict have
    # print(re_dict)

    # 数字字体的对应关系 dict
    font_dict = {}

    for k,v in re_dict.items():
        # put k into  Hexadecimal
        k = '&#x' + hex(k)[2::] + ';'

        # find 'v' reflection
        v = int(v[-2:]) - 1
        # recover the old
        font_dict[k] = str(v)

    # print(font_dict)
    # replace the crawl font
    for k ,v in font_dict.items():
        html = html.replace(k,v)

    print(html)



if __name__ == '__main__':

    text = get_data()
    csrfmiddlewaretoken, pk = deal_data(text)
    # old_code
    html = rebuilt(pk,csrfmiddlewaretoken)
    # use ttf principal deal with the old code into the new code
    deal_ttf(html)


tool.py

# pip install pycryptodome -i https://pypi.tuna.tsinghua.edu.cn/simple
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5 as cry_pksc1_v1_5
import base64

def encrypto(pk, password):
    """
    使用公钥对密码进行加密处理
    :param pk: 公钥
    :param password: 明文密码
    :return: RAS加密之后的密码
    """
    public_key = "-----BEGIN PUBLIC KEY-----\n{}\n-----END PUBLIC KEY-----".format(pk)
    # 导入公钥 返回一个RSA秘钥对象
    rsakey = RSA.importKey(public_key)
    # 对需要加密的内容进行PKCS#1 v1.5加密
    cipher = cry_pksc1_v1_5.new(rsakey)
    # 使用公钥加密密码 密码必须是二进制
    miwen_encode = cipher.encrypt(password.encode())
    # 再使用Base64对类似字节的对象进行编码
    cipher_text = base64.b64encode(miwen_encode).decode()
    return cipher_text


总结:注意找到字体的对应关系然后进行爬取

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

依恋、阳光

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值