Try   HackMD

蝦皮自動簽到

想必大多數的人都有蝦皮帳號吧,不過要每天自己去領取蝦幣真的太麻煩了,所以今天就來教大家建立蝦皮的自動簽到,讓錢錢自己入帳!

你會學到:

  1. selenium:爬蟲套件
  2. cookie 設定:實現免帳密、免認證登入
  3. windows 排程:讓程式自己 run 起來

python & selenium & webdriver

selenium 可以模擬出在瀏覽器的操作行為,我們可以利用 selenium 來領取蝦幣。

selenium

安裝 selenium : pip install selenium

webdriver

使用 selenium 爬蟲前,我們還需 webdriver 來啟動瀏覽器,這裡用 chrome 做示範!也可以下載自己喜歡的瀏覽器的。

  1. 確認 chrome 版本
    點選右上角選單到設定,再點選關於 chrome,就可以看到版本了。

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  2. 下載 Chrome driver
    這個網址 下載相對應的 Chrome driver。

使用 Chrome driver

from selenium import webdriver driver = webdriver.Chrome('path to chrome driver') # 指向 chromedriver 的位置 driver.get('https://shopee.tw/') # 打開瀏覽器視窗,開啟網頁 driver.close() # 關閉瀏覽器視窗

執行程式就會開啟瀏覽器並前往指定的網址了。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

到目前已經可以使用 webdriver 了,我們往下一步邁進!

cookie 會記錄我們的使用資訊,讓我們不用輸入帳密即可登入,但也要保管好 cookie 喔,免得被人拿去壞壞。

from selenium import webdriver import json driver = webdriver.Chrome('path to chrome driver') driver.get('https://shopee.tw/') cookie = driver.get_cookies() # get cookie # 把 cookie 寫入檔案 (json 格式) with open('cookie.json','w') as f: f.write(json.dumps(cookie))

利用 get_cookies() 取得 cookie,不過這時候我們還沒登入,不會有我們的使用資訊,因此將程式修改,等我們登入後再取得 cookie。

from selenium import webdriver import json driver = webdriver.Chrome('path to chrome driver') driver.get('https://shopee.tw/') input() # 登入後再在 terminal 輸入任意字 cookie = driver.get_cookies() # get cookie # 把 cookie 寫入檔案 (json 格式) with open('cookie.json','w') as f: f.write(json.dumps(cookie))

這樣就取得 cookie 了。

from selenium import webdriver import json driver = webdriver.Chrome('path to chrome driver') driver.get('https://shopee.tw/') with open('cookie.json','r') as f: # 讀入 cookie data=json.loads(f.read()) for c in data: driver.add_cookie(c) # 把 cookie 加進去 driver.refresh() # 重新整理

這樣就可以自動登入蝦皮。

自動領取蝦幣

from selenium import webdriver import json driver = webdriver.Chrome("path to chrome driver") driver.get('https://shopee.tw/shopee-coins') # 領取蝦幣的網址 with open('cookie.json','r') as f: data=json.loads(f.read()) for c in data: driver.add_cookie(c) driver.refresh()

這時候我們將網址改成: https://shopee.tw/shopee-coins ,接下來我們要點選領取蝦幣的按鈕。

我們可以按 F12 來取得網頁原始碼,在利用小箭頭選取按鈕,就可以找到相對應的原始碼。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

我們可以看到這個按鈕的 class 是 pcmall-dailycheckin_3uUmyu,利用 webdriver 的 find_element 找到這個按鈕後再按下就可以領取蝦幣了。

from selenium import webdriver from selenium.webdriver.common.by import By import json driver = webdriver.Chrome("path to chrome driver") driver.get('https://shopee.tw/shopee-coins') # 領取蝦幣的網址 with open('cookie.json','r') as f: data=json.loads(f.read()) for c in data: driver.add_cookie(c) driver.refresh() button = driver.find_element(By.CLASS_NAME, 'pcmall-dailycheckin_3uUmyu') # 取得按鈕 (記得 import By) button.click() # 按下按鈕

windows 工作排程器

最後一步,我們要讓程式自己定時執行,這裡介紹 windows 的 工作排程器,直接在搜尋欄位搜尋工作排程器,就可以找到了。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

點選建立工作,輸入相關的資訊就大功告成了!

也歡迎到我的 blog 逛逛呦~

Reference

selenium:
https://steam.oxxostudio.tw/category/python/spider/selenium.html
https://medium.com/marketingdatascience/selenium教學-一-如何使用webdriver-send-keys-988816ce9bed

取得cookie:
https://www.youtube.com/watch?v=bTEpmtIa3z4&t=120s&ab_channel=大數軟體有限公司

windows 排程:
https://pixnashpython.pixnet.net/blog/post/41511724-【win10排程】使用windows排程器讓python自己動起