当前位置:网站首页>手把手教你处理 JS 逆向之图片伪装
手把手教你处理 JS 逆向之图片伪装
2022-07-05 18:40:00 【VIP_CQCRE】
这是「进击的Coder」的第 655 篇技术分享
作者:星安果
来源:AirPython
“
阅读本文大概需要 6 分钟。
”最近在更新反爬系列相关的内容,这一篇就讲讲最简单的「 图片伪装 」
图片伪装是在网页元素中,将文字、图片混合在一起进行展示,以此限制爬虫程序直接获取网页内容
目标对象:
aHR0cHM6Ly93d3cuZ3hyYy5jb20vam9iRGV0YWlsL2Q2NmExNjQxNzc2MjRlNzA4MzU5NWIzMjI1ZWJjMTBi
1 - 分析
打开页面,分析页面发现网页源码中的电话号码默认是隐藏保护的
并且要查看电话号码,必须先通过账号进行登录操作

完成登录后,点击页面上的查看按钮会调用一个接口,随后电话号码就完全展示出来了
https://**/getentcontacts/b2147f6a-6ec7-403e-a836-62978992841b
PS:该 URL 地址中 b2147f6a-6ec7-403e-a836-62978992841b 在网页源码中可以获取,与企业一一对应

通过下图,我们发现上面接口响应值中的「 tel 」字段可以拼接成一张图片,该图片中的内容与电话号码一致
因此,我们只需要下载这张图片,利用 OCR 进行识别即可以

2 - 实现
由于该网站上的文字图片背景很干净,因此不需要额外的训练来提升文字识别率
首先,我们调用接口获取电话号码一一对应的 tel 字段
import requests
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36',
'Cookie': '***'
}
# 获取手机号码对应的tel字段id(一一对应)
def get_tel_id():
# b2147f6a-6ec7-403e-a836-62978992841b对应企业,也是一一对应关系(网页源码)
url = "https://**/getentcontacts/b2147f6a-6ec7-403e-a836-62978992841b"
payload = {}
resp = requests.request("GET", url, headers=headers, data=payload).json()
tel_id = resp.get("tel")
return tel_id然后,利用上面的 tel 字段组成图片 URL 地址
最后,就可以对图片进行文字识别了
这里介绍 2 种方式:
百度 OCR
pytesseract
2-1 百度 OCR
首先,安装依赖包
# 安装依赖包
pip3 install baidu-aip然后,创建一个用于文字识别的应用,获取应用的 APP_ID、API_KEY、SECRET_KEY 数据
最后,参考官方文档调用下面的方法识别图片,获取手机号码数据
官网文档:
https://cloud.baidu.com/doc/OCR/s/wkibizyjk
from aip import AipOcr
def get_phone(tel_id):
"""
百度OCR识别图片,获取文字内容
:param tel_id:
:return:
"""
url = f'https://www.**.com/home/Phone/{tel_id}'
APP_ID = '262**'
API_KEY = '1btP8uUSzfDbji**'
SECRET_KEY = 'NGm6NgAM5ajHcksKs0**'
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
result = client.basicGeneralUrl(url)
# {'words_result': [{'words': '0771-672**'}], 'words_result_num': 1, 'log_id': 1527210***}
print('识别到的手机号码为:', result)2-2 pytesseract
同样,我们需要先安装文字识别、图片处理的依赖包
# 安装依赖包
pip3 install pillow
pip3 install pytesseract然后,根据图片 URL 地址获取图片字节流,最后利用 pytesseract 识别图片中文字即可
import io
import pytesseract
import requests
from PIL import Image
if __name__ == '__main__':
# 获取手机号码的URL地址
image_url = f'https://www.**.com/home/Phone/{get_tel_id()}'
resp = requests.get(image_url, headers=headers)
# images.content: 获取图片的二进制字节流
# io.BytesIO(): 操作处理二进制数据
# Image.open(): 打开图片字节流,得到一个图片对象
images_c = Image.open(io.BytesIO(resp.content))
# 利用pytesseract识别出图片中的字符串,即为手机号码
phone = pytesseract.image_to_string(images_c)
print(f'联系方式: {phone}')以上就是应用图片伪装常规的处理方式,我们只需要找出图片的生成规则,然后利用 OCR 进行识别成文本,最后组装在一起即可

End
崔庆才的新书《Python3网络爬虫开发实战(第二版)》已经正式上市了!书中详细介绍了零基础用 Python 开发爬虫的各方面知识,同时相比第一版新增了 JavaScript 逆向、Android 逆向、异步爬虫、深度学习、Kubernetes 相关内容,同时本书已经获得 Python 之父 Guido 的推荐,目前本书正在七折促销中!
内容介绍:《Python3网络爬虫开发实战(第二版)》内容介绍

扫码购买


点个在看你最好看

边栏推荐
- 企业级数据安全,天翼云是这样理解的
- 一文读懂简单查询代价估算
- Tianyi cloud understands enterprise level data security in this way
- What are the cache interfaces of nailing open platform applet API?
- EasyCVR电子地图中设备播放器loading样式的居中对齐优化
- 解决 contents have differences only in line separators
- The road of enterprise digital transformation starts from here
- 案例分享|金融业数据运营运维一体化建设
- Interviewer: what is the difference between redis expiration deletion strategy and memory obsolescence strategy?
- 线性表——抽象数据类型
猜你喜欢

【历史上的今天】7 月 5 日:Google 之母出生;同一天诞生的两位图灵奖先驱

5年经验Android程序员面试27天,2022程序员进阶宝典

华为让出的高端市场,小米12S靠徕卡能抢到吗?

集合处理的利器

ICML2022 | 长尾识别中分布外检测的部分和非对称对比学习

Mysql database indexing tutorial (super detailed)

MySQL数据库索引教程(超详细)

AI open2022 | overview of recommendation systems based on heterogeneous information networks: concepts, methods, applications and resources

Oracle日期格式转换 to_date,to_char,to_timetamp 相互转换

Various pits of vs2017 QT
随机推荐
cf:B. Almost Ternary Matrix【对称 + 找规律 + 构造 + 我是构造垃圾】
R language Visual scatter plot graph, add labels to some data points in the graph, and always display all labels, even if they have too much overlap. Ggrep package helps
Startup and shutdown of CDB instances
企业级数据安全,天翼云是这样理解的
Shang Silicon Valley Shang preferred project tutorial release
【Autosar 十四 启动流程详解】
2022最新大厂Android面试真题解析,Android开发必会技术
2022 the most complete Tencent background automation testing and continuous deployment practice in the whole network [10000 words]
Why can't Bi software do correlation analysis? Take you to analyze
Oracle date format conversion to_ date,to_ char,to_ Timestamp mutual conversion
Take a look at semaphore, the current limiting tool provided by JUC
如何快速进阶自动化测试?听听这3位BAT大厂测试工程师的切身感想....
Oracle Chinese sorting Oracle Chinese field sorting
图扑软件数字孪生 | 基于 BIM 技术的可视化管理系统
Golang through pointer for Range implements the change of the value of the element in the slice
为什么 BI 软件都搞不定关联分析?带你分析分析
Web3.0时代来了,看天翼云存储资源盘活系统如何赋能新基建(下)
Tianyi cloud understands enterprise level data security in this way
MYSQL中 find_in_set() 函数用法详解
EasyCVR授权到期页面无法登录,该如何解决?