古诗文网模拟登录(使用tesserocr库进行验证码识别)

  • 时间:
  • 来源:互联网
  • 文章标签:
from selenium import webdriver
from lxml import etree
import tesserocr
import numpy as np
import time
from PIL import Image
import matplotlib.pylab as plt
import matplotlib.image as mpimg
#加载谷歌驱动程序
bro=webdriver.Chrome(executable_path='D:\python\Reptiliane\爬虫\chromedriver.exe')
bro.get('https://so.gushiwen.cn/user/login.aspx?')
page_text=bro.page_source
tree=etree.HTML(page_text)
time.sleep(1)
#拿到账号,密码,验证码,的标签
username_tag=bro.find_element_by_id('email')
password_tag=bro.find_element_by_id('pwd')
code=bro.find_element_by_id('code')

#验证码图片进行捕获(裁剪)
bro.save_screenshot('./main.jpg')
time.sleep(1)
img=mpimg.imread('./main.jpg')
# img.show()
# 打开图片
image = plt.imread('main.jpg')
# plt.imshow(image)
# plt.show()
#对验证码图片进行裁剪
img=plt.imshow(image[260:290,500:580])
# plt.show()
print(type(img))
plt.axis('off') # 不显示坐标轴
plt.savefig('./code.jpg')


image=Image.open('code.jpg')
#将图片转化为灰度图像
image=image.convert('L')
threshold=80
table=[]
for i in range(256):
    if i <threshold:
        table.append(0)
    else:
        table.append(1)
#二值化处理
image=image.point(table,'1')
image.save('code2.jpg')
img=Image.open('code2.jpg')
res=tesserocr.image_to_text(img)
time.sleep(1)
print(res)


str1=input('请输入账号:')
username_tag.send_keys(str1)
str2=input('请输入密码:')
password_tag.send_keys(str2)
code.send_keys(res)
time.sleep(3)
bro.close()
print('登录成功!!!')



本文链接http://www.taodudu.cc/news/show-1944662.html