本專案以 Node.js 為基礎,使用爬蟲工具 selenium-webdriver,取得銀行網頁上所需要的匯率資訊,運行後將所獲取的資訊自動存入 Google Sheet 中,並透過 Line Notify 通知結果。另外也包含自動化排程、與背景執行功能。
此專案概念,參考第12屆iThome鐵人賽-行銷廣告、電商小編的武器,FB & IG爬蟲專案從零開始系列文章。更詳細的內容,歡迎閱讀專案介紹、以及專案心得與反思。
$ git clone https://github.com/rubylo718/currency-crawler.git `
$ cd currency-crawler
$ npm install
這個專案使用 Chrome Driver,需要有 Chrome 瀏覽器。
請依照使用者的 Chrome 瀏覽器版本(版本請於瀏覽器中 "關於 Google Chrome" 確認),至 Chrome driver downloads 下載對應版本之 driver,放至專案根目錄中。
SPREADSHEET_ID='your google spreadsheet id'
LINE_NOTIFY_TOKEN='your line notify token'
CRONJOB_TIME='* * * * * *'
說明:
-
操作 Google Sheet 需要憑證,須先至 Google Cloud Project 建立新專案並取得 token,請參考官方文件。
-
使用 Line Notify 功能須至 Line 取得存取 token,請至官方網站。
-
CRONJOB_TIME 是設定自動執行排程的時間參數。例如:
'0 10,15 * * 1-5'
是每週一至週五,10 點 0 分與 15 點 0 分時執行。Cron 時間參數寫法,可以使用 crontab.guru 這個網站,很有幫助 👍
一次性執行:
npm run start
依照所設定的排程執行:
npm run schedule
伺服器會在 http://localhost:8000/ 的位置運行。
下圖為 npm run schedule
在上午 10:10 自動運行之結果:
- Runtime Environment: Node.js @16.14.2
- Framework: Express @4.18.1
- Crawler tool: selenium-webdriver @4.4.0
- Data storage: Google sheet via googleapis @105.0.0
- Scheduling: Cron @2.1.0
- Background process: pm2 @5.2.2
Licensed under the MIT License, Copyright © 2022-present Ruby Lo