Pycharm商业使用激活安装请直接购买。
Pycharm社区版不满足学习需求时可使用专业版试用30天,超过30天还是不满足需求可使用插件激活,仅供学习使用。
具体案例如下:
import re from PIL import Image import pytesseract from seleium import webdriver import time chromedriver.storage.googleapis.com/index.html brew install tesseract brew install tesseract-lang /usr/local/bin export PATH="/usr/local/bin/chromedriver:$PATH" Selenium中的“显示等待”和“隐式等待” z.itpub.net/article/det… www.webkfz.com/python/tssd… blog.csdn.net/Paramete/ar…
# This is a sample Python script.
# Press ⌃R to execute it or replace it with your code.
# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings.
import re
import time
import pytesseract
from PIL import Image, ImageEnhance
from selenium import webdriver
#引入WebDriverWait
from selenium.webdriver.support.ui import WebDriverWait
#引入expected_conditions类,并重命名为EC
from selenium.webdriver.support import expected_conditions as EC
#引入By类
from selenium.webdriver.common.by import By
sWebBaseUrl = "https://kj.czt.gd.gov.cn:8093/"
sChromedriverPath = "./chromedriver"
sAuthCodeSavePath = "./auth_code.png"
sID = "123456"
sPassword = "Aa123456"
sInputID_xPath = "//*[@id='app']/div[1]/div/div/div/div[2]/div[1]/div/div/div[2]/form/div[2]/div/div/input"
sInputPassword_xPath = "//*[@id='app']/div[1]/div/div/div/div[2]/div[1]/div/div/div[2]/form/div[3]/div/div/input"
sInputAuthCode_xPath = "//*[@id='app']/div[1]/div/div/div/div[2]/div[1]/div/div/div[2]/form/div[4]/div/div/div[1]/div/input"
sAuthCodeImage_xPath = "//*[@id='app']/div[1]/div/div/div/div[2]/div[1]/div/div/div[2]/form/div[4]/div/div/div[2]/div/img"
sLoginButton_xPath = "//*[@id='app']/div[1]/div/div/div/div[2]/div[1]/div/div/div[2]/form/div[5]/div/button"
def print_hi(name):
# Use a breakpoint in the code line below to debug your script.
print(f'Hi, {name}') # Press ⌘F8 to toggle the breakpoint.
chrome_browser = webdriver.Chrome(executable_path=sChromedriverPath)
chrome_browser.implicitly_wait(10)
chrome_browser.maximize_window()
chrome_browser.get(sWebBaseUrl)
chrome_browser.find_element_by_xpath(sInputID_xPath).clear()
chrome_browser.find_element_by_xpath(sInputID_xPath).send_keys(sID)
chrome_browser.find_element_by_xpath(sInputPassword_xPath).clear()
chrome_browser.find_element_by_xpath(sInputPassword_xPath).send_keys(sPassword)
chrome_browser.find_element_by_xpath(sInputAuthCode_xPath).clear()
chrome_browser.find_element_by_xpath(sInputAuthCode_xPath).send_keys(sID)
eAuthCodeImage = chrome_browser.find_element_by_xpath(sAuthCodeImage_xPath)
eAuthCodeImage.screenshot(sAuthCodeSavePath)
oAuthCodeImage = Image.open(sAuthCodeSavePath)
oAuthCodeImage = oAuthCodeImage.convert("L") # 转换模式 L | RGB
oAuthCodeImage = ImageEnhance.Contrast(oAuthCodeImage) # 增强对比度
oAuthCodeImage = oAuthCodeImage.enhance(1.0) # 增加饱和度
threshold = 64 # 设定的二值化阈值
table = [] # table是设定的一个表,下面的for循环可以理解为一个规则,小于阈值的,就设定为0,大于阈值的,就设定为1
for i in range(256):
if i < threshold:
table.append(0)
else:
table.append(1)
oAuthCodeImage = oAuthCodeImage.point(table, '1') # 对灰度图进行二值化处理,按照table的规则(也就是上面的for循环)
oAuthCodeImage = delete_spot(oAuthCodeImage)
sAuthCode = pytesseract.image_to_string(oAuthCodeImage)
print(sAuthCode.strip())
oAuthCodeImage.show()
oAuthCodeImage.save(sAuthCodeSavePath)
time.sleep(10)
def delete_spot(image):
data = image.getdata()
w, h = image.size
black_point = 0
for x in range(1, w - 1):
for y in range(1, h - 1):
mid_pixel = data[w * y + x] # 中央像素点像素值
if mid_pixel < 50: # 找出上下左右四个方向像素点像素值
top_pixel = data[w * (y - 1) + x]
left_pixel = data[w * y + (x - 1)]
down_pixel = data[w * (y + 1) + x]
right_pixel = data[w * y + (x + 1)]
# 判断上下左右的黑色像素点总个数
if top_pixel < 10:
black_point += 1
if left_pixel < 10:
black_point += 1
if down_pixel < 10:
black_point += 1
if right_pixel < 10:
black_point += 1
if black_point < 1:
image.putpixel((x, y), 255)
black_point = 0
return image
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
print_hi('PyCharm')
# See PyCharm help at https://www.jetbrains.com/help/pycharm/