OCR识别和Json,PyTesseract库的反爬虫破解

OCR (Optical Character Recognition,光学字符识别)我们可以通过渲染工具 Splash,根据需求将网页中所需的部分数据截图保存,然后再用光学字符识别的手段从截图中提取文字。

1.截取图片的代码

function main(splash, args)
assert(splash:go(args.url))
assert(splash:wait(0.5))

total_png = splash:select(‘content.box .stonefont’):png()
return {
– 将图片信息以键值对的形式返回
total = total_png
}
end

2.在爬虫项目中,将文件对象传给PyTesseract 库

import requests
import json

#Splash 接口
render = ‘实际的url:8050/execute’
url = ‘实际url
#需要执行的命令
script = “”"
function main(splash)
assert(splash:go(’%s’))
assert(splash:wait(0.5))
total_png = splash:select(’.content.box .stonefont’):png()
return {
– 将图片信息以键值对的形式返回
total = total_png
}
end
“”" % url
#设置请求头
header = {‘content-type’: ‘application/json’}
#按照Splash规定提交命令
data = json.dumps({“lua_source”: script})
#向Splash接口发出请求并携带上请求头和命令参数
resp = requests.post(render, data=data, headers=header)
#将Splash返回结果赋值给
images = resp.json()

#把图片保存到Local

import base64
import os
import pytesseract
for key, value in images.items():
# Splash返回的图片使用了base64进行编码,所以我们需要解码
image_body = base64.b64decode(value)
filename = ‘%s.png’ % key
path = os.path.join(os.path.dirname(os.path.abspath(file)), filename)
with open(filename, ‘wb’) as f:
f.write(image_body)
(3) 使用Py

3.Tesseract 库识别指定的图片。
print(pytesseract.image_to_string(filename))

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值