selenium(硒)库与tesseract
2019-08-05 本文已影响0人
喷雾式鲸鱼
2019.8.5
- selenium(读作:舍利尼恩)的核心Selenium Core基于JsUnit,完全由JavaScript编写,因此可以用于任何支持JavaScript的浏览器上。selenium可以模拟真实浏览器,自动化测试工具,支持多种浏览器,爬虫中主要用来解决JavaScript渲染问题。
- selenium需要下载浏览器驱动,这是世界第一浏览器chrome driver 淘宝镜像。
- PhantomJS是无头浏览器无须打开浏览器就可以进行爬虫,但在未来的通知之前,PhantomJS 2.1.1将会是已知最后的稳定版本,其已停止开发,新版本的Selenium不再支持PhantomJS,但可以使用Chrome或Firefox的无头版本来替代,这两个浏览器的无头模式使得PhamtomJS的优势丧失了,所以我们应该转用无头模式。
-
pytesseract
是一个基于google's Tesseract-OCR的API封装包,pytesseract默认支持tiff、bmp格式图片,只有在安装PIL
库之后,才能支持jpeg、gif、png等其他图片格式。要将tesseract添加进环境变量内。 - 一般图像处理验证,需要通过对图像进行灰度处理、二值化后增加图像文字的辨识度。
# 处理图片
def bin(image, threshold):
image = image.convert('L')
table = []
for i in range(256):
if i < threshold:
table.append(0)
else:
table.append(1)
return image.point(table,'1')
- 比较处理前后的识别准确率。图片地址 1.jpg
from PIL import Image
import pytesseract
# tesseract不能直接对网页验证码识别,需要处理,这里用图片
img = Image.open('1.jpg')
result = pytesseract.image_to_string(img, lang='chi_sim') # 默认识别英文和数字,识别简体中文要改参数
print(result)
# 处理图片
def bin(image, threshold):
image = image.convert('L')
table = []
for i in range(256):
if i < threshold:
table.append(0)
else:
table.append(1)
return image.point(table,'1')
image = bin(img, 127)
image.show()
consequence = pytesseract.image_to_string(image, lang='chi_sim') # 输出为字符串,还有其他函数输出更多内容
print(consequence)
result = 'www zhIMA365.coM'
consequence = '町 知 码 网'
- 参考文章:Python之selenium+pytesseract实现识别验证码自动化登陆脚本——卿先生 https://www.cnblogs.com/-qing-/p/11027821.html