iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 5
0
Modern Web

寫給PHP開發者的30堂網路爬蟲開發系列 第 5

Day 5:案例研究 1-1 開爬吧!擷取最新RSS消息實做

前言

我們從Day4可以知道,有兩種方法可以達到我們需要的學校網站上的最新消息。

接下來本章節,就是實做第一個作法的時間了。

實做

在前幾天,有一篇文章講爬蟲建置環境的部份,這時候就可以派上用場了。

首先,我們先透過下列的指令進入先前已經建置好的Docker環境。

docker run -it php_crawler bash

進去之後,先用ls確認有下面這些檔案:

root@f17fccb002eb:~# ls
composer.json  composer.lock  composer.phar  vendor

接著,試著使用 php -v檢查是否有PHP版本存在。如果有的話,就會出現下面的字串。

root@f17fccb002eb:~# php -v
PHP 7.2.22-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Sep  2 2019 12:54:12) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.22-1+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies

接著,在本地端開啟自己熟悉的程式編輯器,把下列的程式碼加入進去,並把程式名稱取名為「lab1-1.php」。

<?php

require_once __DIR__ . '/vendor/autoload.php';

use GuzzleHttp\Client;

$latestNews = 'https://www.nttu.edu.tw/p/503-1000-1009.php';
$client = new Client();
$response = $client->request('GET', $latestNews);

echo (string) $response->getBody();

接著,使用docker ps則會看到下面類似的列表。

docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
06f3a2a2f5e7        php_crawler         "bash"              5 minutes ago       Up 5 minutes                            reverent_keller

就會得知,「06f3a2a2f5e7」是目前剛剛跑起來的Docker container的識別id

接著我們用下列的指令把我們要的PHP檔案傳到Docker container裡面。

docker cp lab1-1.php  06f:/root/

如果網路是通得話,我們就可以抓到下面完整的RSS頻道上的資料了。

當然,每次抓到的都可能不一樣,原因是因為RSS頻道都會更新最新頻道訊息上去。

下面的資料範例則是讓讀者們可以確認應該是要撈到類似像這樣的資料集。

root@06f3a2a2f5e7:~# php lab1-1.php 
<?xml version="1.0" encoding="utf-8" ?><rss version="2.0">
<channel version="2.0">
<title><![CDATA[臺東大學 - 重要消息]]></title>
<link><![CDATA[http://www.nttu.edu.tw]]></link>
<description><![CDATA[僅秘書室與人事室能發布置頂公告]]></description>
<language>utf-8</language>
<pubDate>2019-09-20 23:14:00</pubDate>
<lastBuildDate>2019-09-20 23:14:00</lastBuildDate>
<generator>HeimaVista.com inc </generator>
<item>
<title><![CDATA[【學務處課外組】107學年度第2學期學業績優班級前三名獎學金申請公告]]></title>
<link><![CDATA[https://wdsa.nttu.edu.tw/p/404-1009-51852.php]]></link>
<description><![CDATA[本獎學金與其它獎學金不衝突,無重覆請領疑慮]]></description>
<pubDate>2019-09-19 00:00:00</pubDate>
<author><![CDATA[學生事務處]]></author>
</item>
<item>
<title><![CDATA[【學務處生輔組】忠孝樓遷回一宿申請公告]]></title>
<link><![CDATA[https://wdsa.nttu.edu.tw/p/404-1009-91305.php]]></link>
<description><![CDATA[<p>108學年度第一學期忠孝樓申請搬遷回一宿公告</p>
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse:collapse;width:328px;" width="327">
<colgroup>
<col style="width:85px;" />
<col style="width:243px;" />
</colgroup>
<tbody>
<tr height="68" style="height:68px;">
<td height="68" style="height:68px;width:85px;">說明</td>
<td style="border-left:none;width:243px;">目前一宿因休學、退學、轉學、放棄住宿而釋出床位,因此開放忠孝樓學生申請搬回一宿</td>
</tr>
<tr height="29" style="height:29px;">
<td height="29" style="height:29px;border-top:none;width:85px;">名額</td>
<td style="border-top:none;border-left:none;width:243px;">女生16床男生15床</td>
</tr>
<tr height="22" style="height:22px;">
<td height="22" style="height:22px;border-top:none;width:85px;">房型</td>
<td style="border-top:none;border-left:none;width:243px;">4人房</td>
</tr>
<tr height="44" style="height:44px;">
<td height="44" style="height:44px;border-top:none;width:85px;">申請時間</td>
<td style="border-top:none;border-left:none;width:243px;">9月16日中午12時00分~9...</td></tr></tbody></table>]]></description>
<pubDate>2019-09-16 00:00:00</pubDate>
<author><![CDATA[學生事務處]]></author>
</item>
<item>
<title><![CDATA[【研發處】Language Corner 語言交流夥伴活動~開始接受預約,亦歡迎現場報名唷^^]]></title>
<link><![CDATA[https://rd.nttu.edu.tw/p/404-1007-91275.php]]></link>
<description><![CDATA[<div><strong><span style="color:#0000ff;"><span style="font-size:1.625em;"><span style="background-color:#ffff00;">各位同學大家好:</span></span></span></strong></div>
<div><strong><span style="color:#0000ff;"><span style="font-size:1.625em;"><span style="background-color:#ffff00;">你們有學伴嗎?</span></span></span></strong></div>
<div><strong><span style="color:#0000ff;"><span style="font-size:1.625em;"><span style="background-color:#ffff00;">你們有外國學伴嗎?</span></span></span></strong></div>
<div><strong><span style="color:#0000ff;"><span style="font-size:1.625em;"><span style="background-color:#ffff00;">開學就是要交冰友~</span></span></span></strong></div>
<div><strong><span style="color:#0000ff;"><span style="font-size:1.625em;"><span style="background-color:#ffff00;">請到國際事務中心辦公室(行政大樓一樓電梯後面直直走)</span></span></span></strong><strong><span style="color:#0000ff;"><span style="font-size:1.625em;"><span style="background-color:#ffff00;">填寫你想要的參加的時段~</span></span></span></strong></div>
<div><strong><span style="color:#0000ff;"><span style="font-size:1.625em;"><span style="background-color:#ffff00;"></span></span></span></strong><span style="font-size: 1.625em;"><strong><span style="color:#0000ff;"><span style="background-color:#ffff00;">快點來唷~</span></span></strong></span></div>
<div><span style="font-size: 1.625em;"><br />
<span style="color:#ff0000;"><strong>*俄語與日語地點變更,請已預約的同學暨得到新地點喔!</strong></span></span></div>
<div> </div>
<div> </div>
<div><span style="font-size:1.625em;"><strong>洽詢電話:0...</strong></span></div>]]></description>
<pubDate>2019-09-12 00:00:00</pubDate>
<author><![CDATA[研究發展處]]></author>
</item>
<item>
<title><![CDATA[【研發處】109學年僑生及港澳生單獨招生簡章及申請時間公告(2020年9月入學,限學士班)]]></title>
<link><![CDATA[https://rd.nttu.edu.tw/p/404-1007-91187.php]]></link>
<description><![CDATA[<p>109學年國立臺東大學僑生及港澳生單獨招生</p>
<p>報名繳件時程:2019年10月4日(五)上午9:00起至11月15日(五)下午5:00止</p>
<p> </p>
<p>報名系統 Apply online:<a href="http://isenroll.nttu.edu.tw/" title="報名系統"> http://isenroll.nttu.edu.tw/</a></p>
...]]></description>
<pubDate>2019-09-12 00:00:00</pubDate>
<author><![CDATA[研究發展處]]></author>
</item>
<item>
<title><![CDATA[【研發處】外國學生申請入學(2020年春季班)現正報名中 International Student Admissions(Spring Semester 2020)]]></title>
<link><![CDATA[https://rd.nttu.edu.tw/p/404-1007-91140.php]]></link>
<description><![CDATA[<div><strong><span style="color:#ff0000;"><span style="font-size:1.75em;">2020 春季班報名繳件截止日期  2019 年 9 月 29 日</span></span></strong></div>
<div> </div>
<div><span style="color:#ff0000;"><span style="font-size:1.5em;"><strong>Application Deadlines for Spring Semester : 29 September 2019</strong></span></span></div>
<div> </div>
<div> </div>
<div> </div>
<div><span style="color:#0000ff;"><strong><span style="font-size:1.5em;">報名系統</span></strong></span></div>
<div><span style="color:#0000ff;"><strong><span style="font-size:1.5em;">Apply onlin...</span></strong></span></div>]]></description>
<pubDate>2019-09-09 00:00:00</pubDate>
<author><![CDATA[研究發展處]]></author>
</item>
<item>
<title><![CDATA[【總務處出納組】108學年度第一學期(進修學士班新生)繳交學雜費公告]]></title>
<link><![CDATA[https://dga.nttu.edu.tw/p/404-1008-91078.php]]></link>
<description><![CDATA[<p><iframe frameborder="0" height="800" scrolling="no" src="/var/file/8/1008/img/375/206939753.pdf" width="100%...]]></description>
<pubDate>2019-09-06 00:00:00</pubDate>
<author><![CDATA[總務處]]></author>
</item>
<item>
<title><![CDATA[【教務處】核發108-1舊生續領設籍臺東獎學金公告]]></title>
<link><![CDATA[https://aa.nttu.edu.tw/p/404-1002-90906.php]]></link>
<description><![CDATA[<p><iframe frameborder="0" height="850" scrolling="no" src="/var/file/2/1002/img/1351/500124601.pdf" width="100%"></iframe></p>]]></description>
<pubDate>2019-09-04 00:00:00</pubDate>
<author><![CDATA[教務處]]></author>
</item>
<item>
<title><![CDATA[【教務處】大一優秀新生獎學金、設籍臺東獎學金(含轉學新生)申請公告]]></title>
<link><![CDATA[https://aa.nttu.edu.tw/p/404-1002-90908.php]]></link>
<description><![CDATA[<p><iframe frameborder="0" height="850" scrolling="no" src="/var/file/2/1002/img/1351/399377793.pdf" width="100%"></iframe></p>]]></description>
<pubDate>2019-09-03 00:00:00</pubDate>
<author><![CDATA[教務處]]></author>
</item>
<item>
<title><![CDATA[【教務處】大一新生「運動、美術、音樂」績優獎學金申請公告]]></title>
<link><![CDATA[https://aa.nttu.edu.tw/p/404-1002-90907.php]]></link>
<description><![CDATA[<p><iframe frameborder="0" height="850" scrolling="no" src="/ var/file/2/1002/img/1351/632824446.pdf" width="100%"></iframe></p>]]></description>
<pubDate>2019-09-03 00:00:00</pubDate>
<author><![CDATA[教務處]]></author>
</item>
<item>
<title><![CDATA[【秘書室】東大簡訊-13號刊(20190903)]]></title>
<link><![CDATA[https://enews.nttu.edu.tw/p/404-1045-90881.php]]></link>
<description><![CDATA[<p><iframe frameborder="0" height="900" scrolling="no" src="https://enews.nttu.edu.tw/var/file/45/1045/img/740/433611221.pdf" width="100%"></iframe></p>]]></description>
<pubDate>2019-09-03 00:00:00</pubDate>
<author><![CDATA[東大新聞網]]></author>
</item>
</channel>
<html lang="zh-tw"></html></rss>

今天時間也晚了,明天我們再把擷取下來的文章做解析吧!

參考文獻


上一篇
Day 4:案例研究 1-1 擷取最新消息為例
下一篇
Day 6:案例研究 1-1 解析吧!解析最新RSS消息
系列文
寫給PHP開發者的30堂網路爬蟲開發30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言