爬虫练习(猫眼电影解密)

问题

        随便拿一篇电影做样例。我们发现猫眼的页面数据在预览窗口中全是小方框。在当我们拿到源码以后,数据全是加密后的。所以我们需要想办法破解加密,拿到数据。

破解过程 

        1.源码获取问题与破解

        分析

        在我们刚刚请求url的时候是可以得到数据的,但是过了一段时间后就无法获得数据。虽然状态码为200,但是却没有返回页面源码

一般这种应该是和时间戳有关系,在查看请求负载的时候我们发送,浏览器向这个url不仅发送了时间戳还有一个signKey的密钥。时间戳可以很容易得到,主要问题是如何获得signKey。

        全局搜索signKey,我们发现一段js代码,它的返回值就是我们请求负载的内容。所以需要想办法还原这段js代码。

        分析后发现:

  • d:获取当前时间的函数
  • r:随机数取整
  • c:内容如下method=GET&timeStamp=1725264890773&User-Agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0&index=8&channelId=40011&sVersion=1
    • 可以发现就是多个信息进行拼接(时间戳+User-Agent+index+channelId+sVersion)。
  • f:固定为&key=A013F70DB97834C0A5492378BD76C53A

        分析图片如下:

        同时我们还发现signKey是通过MD5加密(c+f)后得到的。因为1经过MD5加密后得到的内容就是c4ca4238a0b923820dcc509a6f75849b,所以我们可以猜测(0,a[i(_0x140e("0xe4"))])('c+f')就是一个MD5的加密。

         js编写与调用

        有了以上分析后,我们就可以拿页面原始的js代码进行适当的改动。修改后的js代码如下,我们直接返回网页负载需要的params。

        添加首页cookie

        在完成上面步骤后,我们调用js,虽然得到了params,但是还是无法获得到页面的源代码,这可能和cookie有关系,所以我们创建一个session,通过访问首页来保存首页的cookie,然后再来访问这个url看看结果。

        我们发现浏览器请求了两次https://www.maoyan.com/,且第一次存在302跳转,跳转到https://www.maoyan.com/,所以是请求了两次。在python代码中,我们只需要请求有302跳转的链接即可,因为程序会自动进行第二次跳转。

        添加cookie后,使用python程序调用js代码返回params,使用js生成的params去访问url地址运行结果如下:

        2.字体破解

        字体图片下载

        在拿到页面源码以后,我们需要对数字进行获取。直接在返回的源码中搜索,获取.woff文件。得到url://s3plus.meituan.net/v1/mss_73a511b8f91f43d0bdae92584ea6330b/font/e3dfe524.woff,因为每一次请求得到的源码中,woff文件的链接都不同,所以我们需要使用数据提取手段,提取每一次请求得到的woff文件链接并下载保存下来。

        下载并保存woff文件,使用python代码识别woff文件,并保存为图片,识别代码如下,之后会整合到源码中:

from fontTools.ttLib import TTFont
from reportlab.graphics.shapes import Drawing, Path, Group
from reportlab.graphics import renderPM
from reportlab.lib import colors
from reportlab.graphics.shapes import Path

class ReportLabPen(BasePen):
    def __init__(self, glyphSet, path=None):
        BasePen.__init__(self, glyphSet)
        if path is None:
            path = Path()
        self.path = path

    def _moveTo(self, p):
        (x, y) = p
        self.path.moveTo(x, y)

    def _lineTo(self, p):
        (x, y) = p
        self.path.lineTo(x, y)

    def _curveToOne(self, p1, p2, p3):
        (x1, y1) = p1
        (x2, y2) = p2
        (x3, y3) = p3
        self.path.curveTo(x1, y1, x2, y2, x3, y3)

    def closePath(self):
        self.path.closePath()

def ttfToImage(fontName, imagePath, fmt="png"):
    font = TTFont(fontName)  # 打开 WOFF 字体文件
    gs = font.getGlyphSet()
    glyphNames = font.getGlyphNames()[1:]  # 排除第一个 .notdef 字形

    for i in glyphNames:
        g = gs[i]  # 获取当前字形的 Glyph 对象
        pen = ReportLabPen(gs, Path(fillcolor=colors.red, strokeWidth=1))  # 创建 ReportLabPen 对象,并设置相关参数
        g.draw(pen)  # 将当前字形通过 pen 绘制到 path 对象上
        
        # 字形的宽度和高度
        w, h = g.width, g.width + 300  
        g = Group(pen.path)
        g.translate(0, 100)  # 将图形向下移动 100 个像素
        
        d = Drawing(w, h)  # 创建 Drawing 对象,设置宽度和高度
        d.add(g)  # 将 Group 对象添加到 Drawing 对象中
        
        # 定义输出图片路径和文件名
        imageFile = f"{imagePath}/{i}.{fmt}"
        
        # 将 Drawing 对象渲染成图像文件并保存
        renderPM.drawToFile(d, imageFile, fmt)

# 示例用法:将 `mao.woff` 字体文件的字形保存为图像
ttfToImage(fontName="mao.woff", imagePath='images')

        识别结果如下:

 

         识别图片

        识别代码如下,之后会整合到源码中:

import os
import ddddocr  # 导入 ddddocr 库

def orc():
    # 创建一个 ddddocr 的 OCR 对象
    ocr = ddddocr.DdddOcr()
    dicts = {}  # 初始化一个空字典,用于存储识别结果
    lists = os.listdir('./images')  # 获取 images 目录下的所有文件列表
    
    # 遍历每个图片文件
    for imgs in lists:
        # 以二进制模式读取图片文件
        with open('./images/' + imgs, 'rb') as f:
            img_bytes = f.read()
        
        # 使用 OCR 对象的 classification 方法识别图片内容
        res = ocr.classification(img_bytes)
        
        # 输出文件名中提取的 Unicode 代码
        print(222222222222222222, imgs[3:-4])
        
        try:
            # 将文件名中的 Unicode 代码转换为字符,并将识别结果存入字典
            dicts[eval('u\'\\u' + imgs[3:-4].lower() + '\'')] = res
        except:
            # 如果转换或存储过程中出错,则跳过
            pass

        # 打印当前的字典内容
        print(dicts)

# 调用 orc 函数
orc()

        字典输出结果如下:

字典替换

        拿到页面加密的源码,然后根据指点的key来替换掉对应的数字

        替换后的数字与原始页面一样

源码

import requests
import execjs
import re
import shutil
import os
import ddddocr
from fontTools.pens.basePen import BasePen
from fontTools.ttLib import TTFont
from reportlab.graphics.shapes import Drawing, Path, Group
from reportlab.graphics import renderPM
from reportlab.lib import colors
from reportlab.graphics.shapes import Path


class ReportLabPen(BasePen):
    def __init__(self, glyphSet, path=None):
        BasePen.__init__(self, glyphSet)
        if path is None:
            path = Path()
        self.path = path

    def _moveTo(self, p):
        (x, y) = p
        self.path.moveTo(x, y)

    def _lineTo(self, p):
        (x, y) = p
        self.path.lineTo(x, y)

    def _curveToOne(self, p1, p2, p3):
        (x1, y1) = p1
        (x2, y2) = p2
        (x3, y3) = p3
        self.path.curveTo(x1, y1, x2, y2, x3, y3)

    def closePath(self):
        self.path.closePath()


def ttfToImage(fontName, imagePath, fmt="png"):
    font = TTFont(fontName)  # 打开 WOFF 字体文件
    gs = font.getGlyphSet()
    glyphNames = font.getGlyphNames()[1:]  # 排除第一个 .notdef 字形

    for i in glyphNames:
        g = gs[i]  # 获取当前字形的 Glyph 对象
        pen = ReportLabPen(gs, Path(fillcolor=colors.red, strokeWidth=1))  # 创建 ReportLabPen 对象,并设置相关参数
        g.draw(pen)  # 将当前字形通过 pen 绘制到 path 对象上

        # 字形的宽度和高度
        w, h = g.width, g.width + 300
        g = Group(pen.path)
        g.translate(0, 100)  # 将图形向下移动 100 个像素

        d = Drawing(w, h)  # 创建 Drawing 对象,设置宽度和高度
        d.add(g)  # 将 Group 对象添加到 Drawing 对象中

        # 定义输出图片路径和文件名
        imageFile = f"{imagePath}/{i}.{fmt}"

        # 将 Drawing 对象渲染成图像文件并保存
        renderPM.drawToFile(d, imageFile, fmt)


def download_woff():
    with open('猫眼.js','r',encoding='utf-8') as f:
        ctx = execjs.compile(f.read())

    headers_home = {
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
        "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
        "Cache-Control": "max-age=0",
        "Connection": "keep-alive",
        "Sec-Fetch-Dest": "document",
        "Sec-Fetch-Mode": "navigate",
        "Sec-Fetch-Site": "none",
        "Sec-Fetch-User": "?1",
        "Upgrade-Insecure-Requests": "1",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0",
        "sec-ch-ua": "\"Chromium\";v=\"128\", \"Not;A=Brand\";v=\"24\", \"Microsoft Edge\";v=\"128\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\""
    }

    cookies_home = {
        "_lxsdk_s": "191b2c23b90-602-526-0ba%7C%7C1"
    }

    url = "https://www.maoyan.com/"

    s = requests.session()

    # 访问首页,保存cookie
    r = s.get(url, headers=headers_home, cookies=cookies_home)

    headers = {
        "Accept": "*/*",
        "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
        "Connection": "keep-alive",
        "Referer": "https://www.maoyan.com/films/1464004",
        "Sec-Fetch-Dest": "empty",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Site": "same-origin",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0",
        "X-Requested-With": "XMLHttpRequest",
        "sec-ch-ua": "\"Chromium\";v=\"128\", \"Not;A=Brand\";v=\"24\", \"Microsoft Edge\";v=\"128\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\""
    }

    url = "https://www.maoyan.com/ajax/films/1464004"

    params = ctx.call("get_params")

    response = s.get(url, headers=headers, params=params).text

    # 保存woff
    woff_url = "https:" + re.findall(r',url.*?woff', response)[0].split('"')[1]
    woff_res = s.get(woff_url).content
    with open('mao.woff', 'wb') as f:
        f.write(woff_res)
    f.close()

    result = re.findall('<span class="stonefont">(.*?)</span>', response)
    return result


def clear_folder(folder_path):
    # 确保指定路径是一个文件夹
    if os.path.isdir(folder_path):
        # 遍历文件夹中的所有文件和子文件夹
        for filename in os.listdir(folder_path):
            file_path = os.path.join(folder_path, filename)
            try:
                # 如果是文件则删除
                if os.path.isfile(file_path) or os.path.islink(file_path):
                    os.unlink(file_path)
                # 如果是文件夹则删除整个文件夹
                elif os.path.isdir(file_path):
                    shutil.rmtree(file_path)
            except Exception as e:
                print(f"删除 {file_path} 时出错: {e}")
    print("删除完成")


def orc():
    # 创建一个 ddddocr 的 OCR 对象
    ocr = ddddocr.DdddOcr()
    dicts = {}  # 初始化一个空字典,用于存储识别结果
    lists = os.listdir('./images')  # 获取 images 目录下的所有文件列表

    # 遍历每个图片文件
    for imgs in lists:
        # 以二进制模式读取图片文件
        with open('./images/' + imgs, 'rb') as f:
            img_bytes = f.read()

        # 使用 OCR 对象的 classification 方法识别图片内容
        res = ocr.classification(img_bytes)

        # 输出文件名中提取的 Unicode 代码
        print(222222222222222222, imgs[3:-4])

        try:
            # 将文件名中的 Unicode 代码转换为字符,并将识别结果存入字典
            dicts[eval('u\'\\u' + imgs[3:-4].lower() + '\'')] = res
        except:
            # 如果转换或存储过程中出错,则跳过
            pass

    # 返回字典内容
    return dicts


if __name__ == '__main__':
    data = download_woff()

    # 指定要清空的文件夹路径
    folder_path = './images'
    clear_folder(folder_path)

    # 转换 TTF 字体并将字形转换为 PNG 图片
    ttfToImage(fontName="mao.woff", imagePath='images')

    # 使用ocr识别图片,返回字典
    res = orc()

    print(data)
    print(res)
    # 遍历字典并将识别结果输出
    for i in data:
        # 首先去掉所有的 &#x 和 ;
        cleaned_str = i.replace('&#x', '').replace(';', '')

        # 然后进行字符替换
        for key, value in res.items():
            cleaned_str = cleaned_str.replace(key.encode('unicode_escape').decode('ascii').replace('\\u', ''), value)

        print(cleaned_str)


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值