--- title: python 套件介紹 tags: pyautogui, selenium, tesseract, 網路課tcp/ip slideOptions: theme: blood transition: fade --- ![](https://i.imgur.com/rWkxk6E.png) --- ## pyautogui 套件介紹 [pythonGUI自动化操作神器pyautogui](https://www.bilibili.com/video/BV1844y1N7xH?p=1&share_medium=android_i&share_plat=android&share_source=COPY&share_tag=s_i&timestamp=1661664086&unique_k=jFCJriI) --- ``` 前置作業 pip install pyautogui pip install pillow pip install opencv-python ``` ### pyautogui應用-打地鼠 ```python 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") ``` ---- ![](https://i.imgur.com/BCJ1XaG.png) --- ### pyautogui應用-太鼓達人 [影片-太鼓達人](https://youtu.be/u9hpfyzH7hQ) ![](https://i.imgur.com/OLsfrMF.jpg) --- ```python # 太鼓達人遊戲連結 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抢茅台,万能的抢购软件开发!](https://www.bilibili.com/video/BV1gf4y1E7i1?p=1&share_medium=android_i&share_plat=android&share_source=COPY&share_tag=s_i&timestamp=1661664157&unique_k=43Pz6iW) ```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應用-定時自動下單 [影片-在蝦皮上實作](https://youtu.be/LpWi9YGd8Ag) ```python 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 定位頁面元素](http://www.tastones.com/zh-tw/stackoverflow/selenium-webdriver/locating-web-elements/locating_page_elements_using_webdriver/) </br> 尋找鼠標位置及色碼 mouse_location.py ```python= #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) ``` ---- ![](https://i.imgur.com/LBZTpVr.jpg) --- ## tesseract 套件介紹 [Python实现图片验证码识别](https://www.bilibili.com/video/BV1tf4y1P7uD?p=1&share_medium=android_i&share_plat=android&share_source=COPY&share_tag=s_i&timestamp=1661664208&unique_k=78xsVFc) [Tesseract安裝](https://ithelp.ithome.com.tw/articles/10233316) [下載其他語言](https://tesseract-ocr.github.io/tessdoc/Data-Files) ---- ![](https://i.imgur.com/3Ljn8xE.jpg) --- ### tesseract應用-圖片轉文字 ```python 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) ``` --- <h4>輸入圖片</h4> ![](https://i.imgur.com/WnKV81o.png) --- <h4>輸出文字</h4> ``` thls !s a text hello Wworld abcABC ``` --- <h4>輸入圖片</h4> ![](https://i.imgur.com/8nSOO2l.jpg) <h4>輸出文字</h4> ``` 通 是 一 個 中 英 文 混 合 的 sentence﹒ 目 信 這 樣 會 比 較 international。 更 何 況 這 可 以 用 computer 來 辨 識 text 真 的 非 常 good﹒ ``` --- ### 提升辨識度的方法 參考 [Tesseract 使用&安裝&訓練](https://hackmd.io/@DCT/Tesseract-OCR-%E6%96%87%E5%AD%97%E8%BE%A8%E8%AD%98) 1. 至少為300 DPI為最佳 (DPI, Dots Per Inch 每一英吋長度中,取樣或可顯示或輸出點的數目) 2. 白底黑字,字體清晰 3. 去噪,灰度圖二值化 網路上的範例 : [辨識車牌](https://lufor129.medium.com/pytesseract-%E8%BE%A8%E8%AD%98%E5%9C%96%E7%89%87%E4%B8%AD%E7%9A%84%E6%96%87%E5%AD%97-b1024f678fac)