--- tags: 網路課tcp/ip --- # 網路概論筆記 <h2>第一周 2022/09/07</h2> <h1>python pyautogui套件</h1> <h2>1. 打地鼠</h2> [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 ``` ```python= import pyautogui as gui gui.PAUSE = 0.1 # 間格時間避免造成卡頓或來不及打地鼠 gui.FAILSAFE = True # 失效安全防護 while 1: #gui.locateOnScreen(["地鼠圖片"], confidence = [相似度]) coords = gui.locateOnScreen("C:\\Users\\user\\Desktop\\gophers.png", confidence = 0.8) if coords : x,y = gui.center(coords) gui.leftClick(x,y) gui.moveTo(0,0) #避免鼠標遮到地鼠 打完後歸位 else: print("not find") ``` </br></br> <h3>pyautogui應用-太鼓達人</h3> [影片-太鼓達人](https://youtu.be/u9hpfyzH7hQ) ```python= #太鼓達人 https://taiko.bui.pm/ import pyautogui, cv2 from time import sleep, time import keyboard pyautogui.PAUSE = 0.1 listPoint01 = [310,478] 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__": keyboard.wait('a') while 1: click() ``` <h2>2.定時自動下單</h2> [来看我用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("C:\Users\user\Desktop\chromedriver"): driver.find_element_by_xpath("C:\Users\user\Desktop\chromedriver").click() print('') sleep(15) driver.get() 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() 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__': times = input('') driver = webdriver.Chrome() login() but(times) ``` [影片-在蝦皮上實作](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() ``` <h1>python Tesseract套件</h1> <h2>1.圖片轉文字</h2> [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) ```python= from PIL import Image import pytesseract import locale #locale.setlocale(locale.LC_ALL, "C") path="img2.jpg" img = Image.open(path) #result= pytesseract.image_to_string(img, lang='eng') result= pytesseract.image_to_string(img, lang="chi_tra+eng") #lang="chi_tra+eng" print(result) ``` **輸入圖片** ![](https://i.imgur.com/WnKV81o.png) **輸出文字** ![](https://i.imgur.com/ZN8EgCu.png) <hr></hr> **輸入圖片** ![](https://i.imgur.com/8nSOO2l.jpg) **輸出文字** ![](https://i.imgur.com/octxgU8.png) <hr></hr> Windows cmd指令 1.