大家好~我是姐姐恩!身為資訊小白的我,起初對於參賽主題非常苦惱,最後決定利用此次機會,延續學校的課(Java),了解網頁前端三劍客之一的JavaScript!
所以接下來30天,我將在這裡紀錄我當天的學習筆記及統整後的學習內容,請大家多多指教!
*學習內容主要取自MDN Web Docs及彭彭老師的YT課程。
const request = require("request");
const cheerio = require("cheerio");
const puppeteer = require('puppeteer');
const flightCrawler = () => {
const url1="https://tw.trip.com/flights/showfarefirst?lowpricesource=lowPriceCalendar&triptype=RT&class=Y&quantity=1&childqty=0&babyqty=0&jumptype=ChangeSelected&dcity=tpe&acity=sel&ddate=2025-02-07&dcityName=Taipei&acityName=Seoul&rdate=2025-02-13&locale=zh-TW&curr=TWD";
request({
url: url1,
method: "GET"
}, async(error, res, body) => {
// 如果有錯誤訊息,或沒有 body(內容),就 return
if (error || !body) {
return;
}
const data = [];
let $ = cheerio.load(body); // 載入 body
const list = $(".l-inner .m-main-inner .is-v2 .clearfix");
for (let i = 0; i < list.length; i++) {
const airline = list.eq(i).find('.flight-name__wrapper .flights-name').text();
const etd = list.eq(i).find('.is-dep_uA1 .flight-info-airline__timers_Lrt .time_LrR').text();
const eta = list.eq(i).find('.is-arr_uA9 .flight-info-airline__timers_Lrt .time_LrR').text();
const flighttime = list.eq(i).find('.flight-info-stop__segline-wrapper_LrO .flight-info-duration_Lpj').text();
const price = list.eq(i).find('.select-area-price .ThemeColor8').text();
data.push({ airline, etd, eta, flighttime, price });
}
console.log(data);
});
};
flightCrawler();
目前程式碼尚未完成,持續修改中~
學習資源:
https://pala.tw/what-is-web-crawler/
https://ww.wfublog.com/2022/05/nodejs-crawler-basic.html
https://ww.wfublog.com/2022/06/nodejs-google.html