2017年12月26日 星期二

[Node.js] 用 request + cheerio

用 request 抓取頁面 HTML

用 Chrome Developer Tool 觀察 HTML 結構以下為證交所網站資料
<table>
<thead>
<tr>
<th colspan='9'>
<div>105年02月 3008 大立光 各日成交資訊</div>
</th>
</tr>
<tr>
<td>日期</td>
<td>成交股數</td>
<td>成交金額</td>
<td>開盤價</td>
<td>最高價</td>
<td>最低價</td>
<td>收盤價</td>
<td>漲跌價差</td>
<td>成交筆數</td>
</tr>
</thead>
<tbody>
<tr>
<td>105/02/01</td>
<td>2,260,409</td>
<td>5,523,826,200</td>
<td>2,485.00</td>
<td>2,550.00</td>
<td>2,350.00</td>
<td>2,350.00</td>
<td>-150.00</td>
<td>2,164</td>
</tr> ......
把 request 這個 library require 進來之後,就可以用它來發 HTTP request 拿到網頁的 body,參考下面的 code,拿到的 body 是一個字串,就是整個網頁的 HTML
const request = require('request')
const url = 'http://www.twse.com.tw/exchangeReport/STOCK_DAY?response=html&date=20171001&stockNo=3008'
request(url, (err, res, body) => {
  console.log(body)
})

用 cheerio 擷取我們要的部份

拿到 body 之後接著要擷取出要的部份,cheerio 可以用 css selector的語法來選擇元素
const cheerio = require('cheerio')
// 把 body 放進 cheerio 準備分析
const $ = cheerio.load(body)
let stock = []
$('tbody td').each(function(i, elem) {
  sotck.push( $(this).text());
})
console.log(stock)

整理資料

因為拿到的資料滿亂的,稍微把它整理一下並輸出
//欄位0日期
if (i%5==0)
    stock.push( $(this).text()); 

//欄位2當日最高價
if (i%5==2)
   stock.push( parseFloat($(this).text().replace(/,/g,''))); 

輸出暫存

存好後放入txt,db隨便...之後開始賺錢?!技術分析

總結

  • 先找到要抓哪個網站的資料
  • 用 Chrome Developer Tool 觀察 HTML 結構
  • 寫 code 用 request 向網站要資料
  • 把要來的資料用 cheerio 取出要的部份
  • 把資料整理完之後輸出
參考:https://larrylu.blog/nodejs-request-cheerio-weather-414e33f45c7d

0 意見: