Try   HackMD


pyautogui 套件介紹

pythonGUI自动化操作神器pyautogui


前置作業
pip install pyautogui
pip install pillow
pip install opencv-python

pyautogui應用-打地鼠

import pyautogui as pg
pg.PAUSE = 0.1 # 設定pyautogui.PAUSE來控制兩個操作之間的延遲時間。預設情況下,它被設定為0.1 sec
pg.FAILSAFE = True # 失效安全防護
while 1:
            #pg.locateOnScreen(["地鼠圖片"], confidence = [相似度])
    coords = pg.locateOnScreen("C:\\Users\\user\\Desktop\\gophers.png", confidence = 0.8)
    if coords :
        x,y = pg.center(coords)
        pg.leftClick(x,y)
        pg.moveTo(200,300) #避免鼠標遮到地鼠 打完後歸位
    else:
        print("not find")


pyautogui應用-太鼓達人

影片-太鼓達人


# 太鼓達人遊戲連結 https://taiko.bui.pm/
# 本程式參考自https://youtu.be/Nv_wo6D8uwo
import pyautogui, cv2
from time import sleep, time
import keyboard



def click():
    coord01 = pyautogui.pixel(listPoint01[0], listPoint01[1])
    if coord01[0] == 243 and coord01[1] == 71:
        pyautogui.press('j')
    elif coord01[0] == 101 and coord01[1] == 189:
        pyautogui.press('k')
    elif coord01[0] == 243 and coord01[1] == 181:
        pyautogui.press('d')
        pyautogui.press('f')
        pyautogui.press('j')

if __name__ == "__main__":
    pyautogui.PAUSE = 0.1
    listPoint01 = [310,478]
    keyboard.wait('a')
    while 1:
        click()

selenium 套件介紹

来看我用Python抢茅台,万能的抢购软件开发!

#這邊只是架構
from selenium import webdriver
from time import sleep
import time, detetime

def login():
    driver. get('要連到的網址')
    if driver.find_element_by_xpath("網頁上要點擊的按鈕或連結的xpath"):
       #XPath即為XML路徑語言,它是一種用來確定XML檔案中某部分位置的電腦語言。 
       driver.find_element_by_xpath("網頁上要點擊的按鈕或連結的xpath").click()
       print('點擊成功')
       sleep(15)
       driver.get('[link]')
        
    now = datetime.datetime.now()
    print('', now.strftime('%Y-%m-%d %H:%M:%S.%f'))
    if now > tiems:
        while 1:
            try:
                if driver.find_element_by_xpath()
                    driver.find_element_by_xpath().click()
                    break
            except:
                print('X')
    while 1:
        try:
            if driver.find_element_by_xpath():
                driver.find_element_by_xpath().click()
                break
         except:
            print('X')
            
if __name__ == '__main__':
    driver = webdriver.Chrome()
    times = input()
    login()

selenium應用-定時自動下單

影片-在蝦皮上實作

from selenium import webdriver
from time import sleep
import time, datetime
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
import pyautogui as pg


def login():
    driver.get('https://shopee.tw/')
    driver.maximize_window()

    sleep(1.5)
    pg.click(200,600) # close ad
    
    if driver.find_element(By.XPATH, '//*[@id="main"]/div/header/div[1]/nav/ul/a[3]'):#login
        driver.find_element(By.XPATH, '//*[@id="main"]/div/header/div[1]/nav/ul/a[3]').click()
        sleep(0.2)
        
        pg.typewrite('[帳號]', interval=0.01)#input account

        if driver.find_element(By.XPATH, '//*[@id="main"]/div/div[2]/div/div/form/div/div[2]/div[3]/div[1]/input'):
            driver.find_element(By.XPATH, '//*[@id="main"]/div/div[2]/div/div/form/div/div[2]/div[3]/div[1]/input').click()
            sleep(0.1)
            pg.typewrite('[密碼]', interval=0.01)#input password

            if driver.find_element(By.XPATH, '//*[@id="main"]/div/div[2]/div/div/form/div/div[2]/button'):
                driver.find_element(By.XPATH, '//*[@id="main"]/div/div[2]/div/div/form/div/div[2]/button').click()
                sleep(0.5)
                driver.find_element(By.XPATH, '//*[@id="main"]/div/div[2]/div/div/form/div/div[2]/button').click()
                sleep(3)
                driver.get('https://shopee.tw/cart')
            else:
                print("err login")

def buy(times):
    while 1:
        now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
        try:
            if now > times:
                if driver.find_element(By.XPATH, '//*[@id="main"]/div/div[2]/div/div/div[3]/div[1]/div[2]/div[1]/label/div'): #點擊全選
                    driver.find_element(By.XPATH, '//*[@id="main"]/div/div[2]/div/div/div[3]/div[1]/div[2]/div[1]/label/div').click()
                    break
            else:
                print("時間未到",end="")
        except:
            print("select all not find")
            
    if driver.find_element(By.XPATH, '//*[@id="main"]/div/div[2]/div/div/div[3]/div[2]/div[7]/button[2]'): # 去買單
        driver.find_element(By.XPATH, '//*[@id="main"]/div/div[2]/div/div/div[3]/div[2]/div[7]/button[2]').click()
        if driver.find_element(By.XPATH,'//*[@id="main"]/div/div[2]/div/div/div[3]/div[2]/div[7]/button[2]'):
            #driver.find_element(By.XPATH, '').click()
            print("\n成功")


if __name__ == '__main__':
    now = datetime.datetime.now()
    print('現在時間是:', now.strftime('%Y-%m-%d %H:%M:%S.%f'))
    times = input('請輸入搶購時間:')
    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
    login()
    buy(times)
    sleep(30)
    driver.close()

使用 WebDriver 定位頁面元素


尋找鼠標位置及色碼
mouse_location.py

#mouse_location.py import pyautogui as pg def rgb2hex(r, g, b): return '#{:02x}{:02x}{:02x}'.format(r, g, b) while 1: pos = pg.position() print(pos,end="") x, y = pg.position() rgb = pg.screenshot().getpixel((x, y)) r = str(rgb[0]).rjust(3) g = str(rgb[1]).rjust(3) b = str(rgb[2]).rjust(3) hex_c = rgb2hex(int(r),int(g),int(b)) color_str = f'選中顏色 RGB:({r},{g},{b}), 16進位:{hex_c}' print(color_str)


tesseract 套件介紹

Python实现图片验证码识别

Tesseract安裝
下載其他語言



tesseract應用-圖片轉文字

from PIL import Image
import pytesseract

path="img2.jpg"

img = Image.open(path)

#lang='eng'
result= pytesseract.image_to_string(img, lang="chi_tra+eng")


print(result)

輸入圖片


輸出文字

thls !s a text
hello Wworld

abcABC

輸入圖片

輸出文字

通 是 一 個 中 英 文 混 合 的 sentence﹒

目 信 這 樣 會 比 較 international。

更 何 況 這 可 以 用 computer 來 辨 識 text
真 的 非 常 good﹒

提升辨識度的方法

參考 Tesseract 使用&安裝&訓練

  1. 至少為300 DPI為最佳 (DPI, Dots Per Inch 每一英吋長度中,取樣或可顯示或輸出點的數目)
  2. 白底黑字,字體清晰
  3. 去噪,灰度圖二值化

網路上的範例 : 辨識車牌