輕鬆學習 Python:定時執行網站爬蟲

[yes|no]## [no] >>> yes安裝結束後,以 exit 指令離開 EC2,接著再以 ssh -i 指令重新連線,這時再以 which python 檢視可以發現系統已經改以 Miniconda 的 Python 作為預設。## /home/ubuntu/miniconda3/bin/python為了能夠讓 EC2 順利執行爬蟲程式,我們要安裝程式所使用的 Site-Packages:requests、beautifulsoup4 與 pandas 。Downloading and Extracting Packagesmkl_fft-1.

0.

12 | 172 KB | ################################### | 100% intel-openmp-2019.

4 | 876 KB | ################################### | 100% openssl-1.

1.

1c | 3.

8 MB | ################################### | 100% numpy-base-1.

16.

4 | 4.

4 MB | ################################### | 100% mkl_random-1.

0.

2 | 405 KB | ################################### | 100% python-dateutil-2.

8.

| 281 KB | ################################### | 100% blas-1.

0 | 6 KB | ################################### | 100% libgfortran-ng-7.

3.

0 | 1.

3 MB | ################################### | 100% mkl-2019.

4 | 204.

1 MB | ################################### | 100% beautifulsoup4-4.

7.

1 | 143 KB | ################################### | 100% requests-2.

22.

0 | 89 KB | ################################### | 100% pandas-0.

24.

2 | 11.

1 MB | ################################### | 100% numpy-1.

16.

4 | 49 KB | ################################### | 100% soupsieve-1.

8 | 104 KB | ################################### | 100% pytz-2019.

1 | 236 KB | ################################### | 100% ca-certificates-2019 | 133 KB | ################################### | 100% Preparing transaction: doneVerifying transaction: doneExecuting transaction: done部署爬蟲程式EC2 的環境已經準備妥當,在開始定期執行之前還有兩件事情要做:微調一些爬蟲程式:增加把資料框寫入 SQLite 資料庫的程式調整 EC2 的時區:將預設的 UTC+0 時區調整為台灣台北時間(UTC/GMT +8)使用資料框的 to.

sql() 方法可以寫入指定的資料庫表格,參數 if_exists=’append’ 設定當表格存在時以添加觀測值的方式更新。接著以 timedatectl 指令檢查目前 EC2 的時區。## Local time: Sun 2019-06-09 06:54:02 UTC## Universal time: Sun 2019-06-09 06:54:02 UTC## RTC time: Sun 2019-06-09 06:54:03## Time zone: Etc/UTC (UTC, +0000)## System clock synchronized: yes## systemd-timesyncd.

service active: yes## RTC in local TZ: no預設的時區是 UTC+0,是倫敦格林尼治時間;調整為台灣台北時間(UTC/GMT+8)只要安裝 tzdata 套件,在互動安裝的過程中選擇亞洲、台北時區。安裝完成以後再執行 timedatectl 指令檢查目前 EC2 的時區。## Local time: Sun 2019-06-09 15:01:01 CST## Universal time: Sun 2019-06-09 07:01:01 UTC## RTC time: Sun 2019-06-09 07:01:02## Time zone: Asia/Taipei (CST, +0800)## System clock synchronized: yes## systemd-timesyncd.

service active: yes## RTC in local TZ: no使用任何一種 FTP 軟體把爬蟲程式(命名為 price_rank_scraper.

py)上傳至 EC2 的家目錄下 /home/ubuntu 。我們希望這支爬蟲程式在每天的 9:30 到 16:30 之間每小時都執行一次,可以 crontab -e 指令在定時執行的程式中加入:30 9-16 * * * /home/ubuntu/miniconda3/bin/python /home/ubuntu/price_rank_scraper.

py小結在這個小節中我們簡介如何以 Python 的 requests、beautifulsoup4 套件擷取網頁資料;pandas、sqlite3 套件寫入 SQLite 資料庫,並且整合成為一個爬蟲程式;最後建立了一個 AWS EC2 元件透過 crontab 來定時執行這個爬蟲程式。延伸閱讀Getting Started with Amazon EC2 Linux Instances – Amazon Elastic Compute CloudGet started with Amazon EC2 by launching, connecting to, and using a Linux instance.

docs.

aws.

amazon.

comSetting the Time for Your Linux Instance – Amazon Elastic Compute CloudSet the time or change the time zone for an Amazon Linux instance.

docs.

aws.

amazon.

comUbuntu 16.

04 改變 Timezone 時區設定Ubuntu 要改變 Timezone 時區設定,可以用以下方法:dpkg-reconfigure用 root 身份用 dpkg-reconfigure 設定 Ubuntu 的 Timezone…www.

opencli.

com利用 crontab 來做 Linux 固定排程 | kpman | code近期有個需求,要在 Linux 上執行固定週期的時程,發現利用 crontab 這個內建的功能便可以完成,本篇記錄使用過程以及相關的參數。 crontab 介紹 crontab 是 Linux.

code.

kpman.

cc.. More details

Leave a Reply