使用谷歌浏览器chrome自动登录网页,下载chromedriver.exe并放到项目目录下,选择自己谷歌浏览对应的版本 http://npm.taobao.org/mirrors/chromedriver/
1、使用之前必须要先安装第三方扩展库
pip install selenium
pip install Image
pip install baidu-aip
pip install pyinstaller # 安装pyinstaller,打包exe文件需要用到
# pyinstaller -F login.py 把python文件打包成exe文件,加上-w小写可以屏蔽黑窗口
2、封装百度云接口为baidu.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
from aip import AipOcr
'''
百度云
'''
APP_ID = '百度云获取'
API_KEY = '百度云获取'
SECRET_KEY = '百度云获取'
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
# 参考百度云接口说明
class baiDu:
# {'log_id': 4411569300744191453, 'words_result_num': 1, 'words_result': [{'words': ' Y7Y.: 4'}]}
def __init__(self, filePath='', options='', url='', idCardSide='front'):
# 图片完整URL,URL长度不超过1024字节,URL对应的图片base64编码后大小不超过4M,
# 最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式,当image字段存在时url字段失效
self.url = url
# 本地图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
self.image = ''
# 可选参数
self.options = options
# front:身份证含照片的一面;back:身份证带国徽的一面
self.idCardSide = idCardSide
if filePath:
# 读取图片
with open(filePath, 'rb') as fp:
self.image = fp.read()
def wordGeneral(self):
""" 调用通用文字识别, 50000次/天免费 """
return client.basicGeneral(self.image, self.options) # 还可以使用身份证驾驶证模板,直接得到字典对应所需字段
def wordGeneralUrl(self):
""" 调用通用文字识别, 图片参数为远程url图片 500次/天免费 """
return client.basicGeneralUrl(self.url, self.options)
def wordGeneralLocation(self):
""" 调用通用文字识别(含位置信息版), 图片参数为本地图片 500次/天免费 """
return client.general(self.url, self.options)
def wordGeneralUrlLocation(self):
""" 调用通用文字识别, 图片参数为远程url图片 500次/天免费 """
return client.generalUrl(self.url, self.options)
def wordAccurate(self):
""" 调用通用文字识别(高精度版) 500次/天免费 """
return client.basicAccurate(self.image, self.options)
def wordAccurateLocation(self):
""" 调用通用文字识别(含位置高精度版) 50次/天免费 """
return client.accurate(self.image, self.options)
def wordEnhancedGeneral(self):
""" 调用通用文字识别(含生僻字版), 图片参数为本地图片 """
return client.enhancedGeneral(self.image, self.options)
def wordEnhancedGeneralUrl(self):
""" 调用通用文字识别(含生僻字版), 图片参数为远程url图片 """
return client.enhancedGeneralUrl(self.image, self.options)
def wordIdcard(self):
""" 调用身份证识别, 图片参数为本地图片 front:身份证含照片的一面;back:身份证带国徽的一面 """
return client.idcard(self.image, self.idCardSide, self.options)
def wordBankcard(self):
""" 调用银行卡识别 """
return client.bankcard(self.image)
def wordDrivingLicense(self):
""" 调用驾驶证识别 """
return client.drivingLicense(self.image, self.options)
def wordVehicleLicense(self):
""" 调用行驶证识别 """
return client.vehicleLicense(self.image, self.options)
def wordLicensePlate(self):
""" 调用车牌识别 """
return client.licensePlate(self.image, self.options)
def wordBusinessLicense(self):
""" 调用营业执照识别 """
return client.businessLicense(self.image, self.options)
def wordReceipt(self):
""" 调用通用票据识别 """
return client.idcard(self.image, self.options)
def ImageRecognition(self):
""" 调用通用物体识别 """
return client.advancedGeneral(self.image, self.options)
3、新建login.py文件
#!/usr/bin/python
# -*- coding: utf-8 -*-
# pip install selenium
# pip install Image
# pip install baidu-aip
# pip install pyinstaller 安装pyinstaller,打包exe文件需要用到
# pyinstaller -F login.py 把python文件打包成exe文件,加上-w小写可以屏蔽黑窗口
import os
import re
import time
import baidu
from selenium import webdriver
from PIL import Image
i = 0
# 获取项目目录
path = os.path.abspath(os.path.dirname(__file__))
loginImage = path + "/login.png" # 登录页面截图
codeImage = path + "/code.png" # 定位验证码截图
base_url = '登录页URL地址'
browser = webdriver.Chrome()
browser.maximize_window()
browser.implicitly_wait(10)
browser.get(base_url)
# (2)基操
browser.find_element_by_id("username").send_keys("admin")
browser.find_element_by_id("password").send_keys("admin")
# time.sleep(2)
# 截图方法
def code_jt():
# 登录页面截图
browser.save_screenshot(loginImage) # 可以修改保存地址
# 获取图片验证码坐标
code_ele = browser.find_element_by_xpath('//img[@style="cursor:pointer;"]')
# print("验证码的坐标为:", code_ele.location)
# print("验证码的大小为:", code_ele.size)
# 图片4个点的坐标位置
left = code_ele.location['x'] # x点的坐标
top = code_ele.location['y'] # y点的坐标
right = code_ele.size['width'] + left # 上面右边点的坐标
down = code_ele.size['height'] + top # 下面右边点的坐标
image = Image.open(loginImage)
# 将图片验证码截取
code_image = image.crop((left, top, right, down))
code_image.save(codeImage) # 截取的验证码图片保存为新的文件
image = Image.open(codeImage)
img = image.convert('L') # P模式转换为L模式(灰度模式默认阈值127)
count = 165 # 设定阈值
table = []
for m in range(256):
if m < count:
table.append(0)
else:
table.append(1)
img = img.point(table, '1')
img.save(codeImage) # 保存处理后的验证码
def bd_img(i=0):
# 调用百度云接口
api = baidu.baiDu(codeImage)
""" 调用通用文字识别, 50000次/天免费 """
results = api.wordGeneral()
# print(results)
for result in results["words_result"]:
text = result["words"]
text = text.strip()
text = re.sub(r'[^A-Za-z0-9]+', '', text) # 替换特殊字符
if len(text) == 6:
print("正确" + str(i) + ":"+text)
# 添加验证码信息
browser.find_element_by_id("code").send_keys(text)
# 提交按钮查看按钮元素,click模拟点击提交
browser.find_element_by_xpath('//input[@type="submit"]').click()
time.sleep(5) # 等待5秒,等待网页加载完成
# 登录后就可以试下点击签到,打卡功能了
# links = browser.find_element_by_xpath('//a[@class="checkin"]')
# links.click()
else:
print("错误" + str(i) + ":" + text)
# 提交按钮查看按钮元素,click模拟点击提交
browser.find_element_by_xpath('//img[@style="cursor:pointer;"]').click()
if i > 10:
print('验证码获取失败')
exit(1)
else:
i += 1
code_jt()
bd_img(i)
code_jt()
bd_img(i)
3.1、(备选方案)使用pytesseract识别文字(先安装:pip install pytesseract)
如果报错:pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it‘s not in your PATH
参考:https://blog.csdn.net/qq_31362767/article/details/107891185 (下载 tesseract.exe并安装,修改
pytesseract.py文件的pytesseract_cmd的值)
# 使用pytesseract识别文字
def pytesseract_img(i):
# 设置为中文文字的识别 lang='chi_sim'; #设置为英文或阿拉伯字母的识别 lang='eng'
text = pytesseract.image_to_string(Image.open(codeImage), lang='eng')
text = re.sub(r'[^A-Za-z0-9]+', '', text) # 替换特殊字符 re.match("^[a-zA-Z0-9]*$", text) and
if len(text) == 6:
print("正确" + str(i) + ":" + text)
browser.find_element_by_id("code").send_keys(text)
browser.find_element_by_xpath('//input[@type="submit"]').click()
time.sleep(2) # 等待5秒,等待网页加载完成
try:
# 如果获取到验证码,可知验证验证错误
code = browser.find_element_by_id("code").get_attribute('value')
if code:
# 刷新验证码
browser.find_element_by_xpath('//img[@style="cursor:pointer;"]').click()
code_jt()
i += 1
# 验证码内容置空
browser.find_element_by_id("code").clear()
browser.find_element_by_id("username").send_keys("admin")
browser.find_element_by_id("password").send_keys("admin")
pytesseract_img(i)
except:
print('登录成功')
exit(1)
# links = browser.find_element_by_xpath('//a[@class="checkin"]') # 点击签到
# links.click()
else:
print("错误" + str(i) + ":" + text)
browser.find_element_by_xpath('//img[@style="cursor:pointer;"]').click()
i += 1
code_jt()
pytesseract_img(i)
if i > 20:
print('验证码获取失败')
exit(1)
4、打包程序
在py程序所在文件夹,打开cmd,输入pyinstaller -F login.py,打包为一个可运行程序,然后使用window的计划任务,就可以实现,定时签到,点赞等登录后的操作。
pyinstaller -D -w -i "D:\wwwroot\python\login\favicon.ico" login.py
如运行后报错:struct.error: unpack requires a buffer of 16 bytes
ico图标在线转换一下就可以了
pyinstaller相关参数
-F:生成一个文件夹,里面是多文件模式,启动快。
-D:仅仅生成一个文件,不暴露其他信息,启动较慢。
-w:窗口模式打包,不显示控制台。
-i:跟图标路径,作为应用icon。
5、Pyinstaller打包selenium去除chromedriver黑框问题解决!!!
解决方案就是修改selenium包中的service.py第76行
D:\wwwroot\python\Lib\site-packages\selenium\webdriver\common\service.py
try:
cmd = [self.path]
cmd.extend(self.command_line_args())
self.process = subprocess.Popen(cmd, env=self.env,
close_fds=platform.system() != 'Windows',
stdout=self.log_file,
stderr=self.log_file,
stdin=PIPE)
修改为:
try:
cmd = [self.path]
cmd.extend(self.command_line_args())
self.process = subprocess.Popen(cmd, env=self.env,
close_fds=platform.system() != 'Windows',
stdout=self.log_file,
stderr=self.log_file,
stdin=PIPE, creationflags=134217728)