Python爬虫实例之2021猫眼票房字体加密反爬策略(粗略版)

yizhihongxing

下面我会给出完整的攻略,请认真阅读。

1. 前置知识要求

在学习本篇攻略之前,需要对以下内容有一定的了解:

  • Python基础知识
  • 网络爬虫基础知识
  • 字体反爬机制及解决方案

如果您对以上内容并不熟悉,建议先学习相关知识再来阅读本篇攻略。

2. 需求分析

我们的目标是爬取2021猫眼电影票房榜单,并将结果存储到本地文件中。但是,猫眼电影网站进行了字体加密反爬策略,我们需要解决这个问题。

3. 技术选型

  • Python 3.X
  • requests 库
  • lxml 库
  • fontTools 库

4. 实现步骤

  1. 确定目标URL

我们要爬取的是2021年每日的猫眼电影票房榜单,可以选择以下URL:

https://box.maoyan.com/promovie/api/box/second.json
  1. 获取字体文件

打开该URL并观察网页源代码,发现其中有一串字体文件的URL:

https://vfile.meituan.net/colorstone/4f6db24e59331a9d59588cfcce393ccf3294.woff

将该URL保存到本地,这就是我们需要的字体文件。

  1. 解析字体文件

我们可以使用fontTools库来解析字体文件,这样就能将加密的字体转换成可读的文字。

下面是使用fontTools库解析字体文件的示例代码:

from fontTools.ttLib import TTFont

font = TTFont('font.woff')

uni_list = font['cmap'].tables[0].ttFont.getGlyphOrder()

uni_dict = {}

for uni in uni_list[1:]:
    obj = font['glyf'][uni]
    uni_dict[uni] = obj.coordinates

print('字体编码值和坐标:', uni_dict)
  1. 发送网络请求并解析数据

发送GET请求,将获取到的数据使用lxml库进行解析。

下面是示例代码:

import requests
from lxml import etree

url = 'https://box.maoyan.com/promovie/api/box/second.json'

headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36'
}

response = requests.get(url, headers=headers)

data = response.json()['data']['list']

ids = []
names = []
for item in data:
    ids.append(item['movieId'])
    name = ''
    for c in item['movieName']:
        name += chr(uni_dict[c][1] // 3 - 7)
    names.append(name)

print('电影ID:', ids)
print('电影名称:', names)
  1. 存储到本地

将获取到的每日电影票房数据存储到本地文件中,可以使用Python内置的文件操作函数来实现。

下面是示例代码:

with open('maoyan.txt', 'a', encoding='utf-8') as f:
    for i in range(len(ids)):
        f.write(str(ids[i]) + ' ' + names[i] + '\n')

5. 示例

下面我将给出两个示例:

示例1:爬取2021年10月1日电影票房并保存到本地文件

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

url = 'https://box.maoyan.com/promovie/api/box/second.json'

headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36'
}

response = requests.get(url, headers=headers)

data = response.json()['data']['list']

# 解析字体文件
font_url = 'https://vfile.meituan.net/colorstone/4f6db24e59331a9d59588cfcce393ccf3294.woff'
response_font = requests.get(font_url)
with open('font.woff', 'wb') as f:
    f.write(response_font.content)
font = TTFont('font.woff')
uni_list = font['cmap'].tables[0].ttFont.getGlyphOrder()
uni_dict = {}
for uni in uni_list[1:]:
    obj = font['glyf'][uni]
    uni_dict[uni] = obj.coordinates

ids = []
names = []
for item in data:
    ids.append(item['movieId'])
    name = ''
    for c in item['movieName']:
        name += chr(uni_dict[c][1] // 3 - 7)
    names.append(name)

# 保存到本地文件
with open('maoyan.txt', 'w', encoding='utf-8') as f:
    for i in range(len(ids)):
        f.write(str(ids[i]) + ' ' + names[i] + '\n')

示例2:将每日电影票房数据保存到MySQL数据库中

import requests
from lxml import etree
from fontTools.ttLib import TTFont
import pymysql

url = 'https://box.maoyan.com/promovie/api/box/second.json'

headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36'
}

response = requests.get(url, headers=headers)

data = response.json()['data']['list']

# 解析字体文件
font_url = 'https://vfile.meituan.net/colorstone/4f6db24e59331a9d59588cfcce393ccf3294.woff'
response_font = requests.get(font_url)
with open('font.woff', 'wb') as f:
    f.write(response_font.content)
font = TTFont('font.woff')
uni_list = font['cmap'].tables[0].ttFont.getGlyphOrder()
uni_dict = {}
for uni in uni_list[1:]:
    obj = font['glyf'][uni]
    uni_dict[uni] = obj.coordinates

ids = []
names = []
for item in data:
    ids.append(item['movieId'])
    name = ''
    for c in item['movieName']:
        name += chr(uni_dict[c][1] // 3 - 7)
    names.append(name)

# 保存到MySQL数据库中
conn = pymysql.connect(host='localhost', user='root', password='123456', database='test', charset='utf8mb4')
cursor = conn.cursor()
for i in range(len(ids)):
    sql = "INSERT INTO maoyan(id, name) VALUES (%s, '%s')" % (ids[i], names[i])
    try:
        cursor.execute(sql)
        conn.commit()
    except:
        conn.rollback()
cursor.close()
conn.close()

以上就是“Python爬虫实例之2021猫眼票房字体加密反爬策略(粗略版)”的详细攻略,希望对你有所帮助。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Python爬虫实例之2021猫眼票房字体加密反爬策略(粗略版) - Python技术站

(0)
上一篇 2023年5月20日
下一篇 2023年5月20日

相关文章

  • python 中的requirements.txt 文件的使用详情

    下面是“Python 中的 requirements.txt 文件的使用详情”的完整攻略: 什么是 requirements.txt 文件? 在 Python 中,使用第三方库是很常见的事情,但是如果你要在多个环境中使用这些相同的库,一个个手动安装常常会十分繁琐。这时就需要使用 requirements.txt 文件,它可以记录你的项目依赖的所有库及其版本号…

    python 2023年6月5日
    00
  • Python 分析访问细节

    Python可以利用各种库和工具对网站的访问细节进行分析和解析,以了解有关网站性能和使用情况的详细信息。本文将介绍使用Python进行网站访问分析的完整攻略。 准备工作 在开始Python分析网站访问细节之前,需要安装并导入必要的库和工具。常用的库和工具包括: requests:发送HTTP请求以获取访问网站的响应。 Beautiful Soup:解析HTM…

    python-answer 2023年3月25日
    00
  • Python中三种时间格式转换的方法

    当涉及到时间处理时,经常需要进行时间格式转换。在Python中有三种时间格式:时间戳、字符串、元组。本文将分享三种时间格式之间的相互转换方法,方便开发者在实际工作中灵活应用。 一、时间戳与字符串的互相转换 1.将时间戳转换成字符串 下面是一个将时间戳转换成字符串的例子: import time timestamp = 1528797322 # 将时间戳转换成…

    python 2023年6月2日
    00
  • 如何在 f-string 中使用换行符 ‘\n’ 来格式化 Python 3.6 中的输出?

    【问题标题】:How to use newline ‘\n’ in f-string to format output in Python 3.6?如何在 f-string 中使用换行符 ‘\n’ 来格式化 Python 3.6 中的输出? 【发布时间】:2023-04-04 23:34:02 【问题描述】: 我想知道如何使用 f-strings 以 Pyt…

    Python开发 2023年4月6日
    00
  • python把数组中的数字每行打印3个并保存在文档中的方法

    要将Python中的数组中的数字每行打印3个并保存在文档中,可以按照以下步骤进行: 第一步:创建一个数组 在 Python 中,可以用以下语句创建一个包含数字的数组: my_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] 第二步:迭代数组并打印每行3个数字 代码如下: count = 0…

    python 2023年6月6日
    00
  • 简单的Python解密rsa案例

    下面是对题目的详细解答: 标题 首先,在回答前需要确定题目的标题为“简单的Python解密RSA案例的完整攻略”。 简介 RSA加密算法是一种常见的非对称加密算法,其加密和解密过程都需要使用到密钥,其中公钥可以公开,私钥需要保密,以保证信息的安全性。本文将介绍如何使用Python对RSA算法进行解密,并提供代码示例说明。 思路 在进行RSA解密时,需要使用到…

    python 2023年6月3日
    00
  • Python如何输出警告信息

    下面是Python输出警告信息的完整攻略: 1. 使用 warnings 模块 warnings 是Python的一个标准模块,可以用来发出警告信息。该模块提供了进一步的控制,还可以选择将警告转换成异常,以便在代码中引发问题。 示例1: 输出简单警告信息 import warnings def my_function(x): if x < 0: war…

    python 2023年5月13日
    00
  • python处理RSTP视频流过程解析

    RTSP(Real Time Streaming Protocol)是一种用于实时数据传输的网络协议。在Python中,可以使用OpenCV库处理RTSP视频流。以下是Python处理RTSP视频流的详细攻略: 安装OpenCV库 要处理RTSP视频流,需要安装OpenCV库。可以使用pip安装OpenCV库。以下是安装OpenCV库的示例: pip ins…

    python 2023年5月14日
    00
合作推广
合作推广
分享本页
返回顶部