LoginSignup

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 5 years have passed since last update.

LINE BOT開発 IGRスクレイピング編

Last updated at Posted at 2017-05-13

スクレイピング: Webサイトから情報を"盗って"くること

http://www.igr.jp/ このページの情報を盗ってきましょう。

axiosのインストール

npm i --save axios
igr.js
'use strict'

const axios = require('axios');
const URL = `http://www.igr.jp/`;

axios.get(URL, {
    firstName: 'Fred',
    lastName: 'Flintstone'
  })
  .then((response) => {
    console.log(response.data);
  })
  .catch((error) => {
    console.log(error);
  });

実行

node igr.js

実際に取得してみよう

igr.js
'use strict'

const axios = require('axios');
const URL = `http://www.igr.jp/`;

axios.get(URL, {
    firstName: 'Fred',
    lastName: 'Flintstone'
  })
  .then((response) => {
    // console.log(response.data);
    let item = response.data;
    let status = item.match(/operatinginfo"><strong>(.*?)<\/strong>/)[1];
    console.log(status);
  })
  .catch((error) => {
    console.log(error);
  });
  • 実行
$ node igr.js
平常通り運行しております

とりあえずのコード

'use strict';

const http = require('http');
const https = require('https');
const crypto = require('crypto');

const HOST = 'api.line.me'; 
const REPLY_PATH = '/v2/bot/message/reply';//リプライ用
const CH_SECRET = ''; //Channel Secretを指定
const CH_ACCESS_TOKEN = ''; //Channel Access Tokenを指定
const SIGNATURE = crypto.createHmac('sha256', CH_SECRET);
const PORT = 3000;

const axios = require('axios');
const URL = `http://www.igr.jp/`;

/**
 * httpリクエスト部分
 */
const client = (replyToken, SendMessageObject) => {    
    let postDataStr = JSON.stringify({ replyToken: replyToken, messages: SendMessageObject });
    let options = {
        host: HOST,
        port: 443,
        path: REPLY_PATH,
        method: 'POST',
        headers: {
            'Content-Type': 'application/json; charset=UTF-8',
            'X-Line-Signature': SIGNATURE,
            'Authorization': `Bearer ${CH_ACCESS_TOKEN}`,
            'Content-Length': Buffer.byteLength(postDataStr)
        }
    };

    return new Promise((resolve, reject) => {
        let req = https.request(options, (res) => {
                    let body = '';
                    res.setEncoding('utf8');
                    res.on('data', (chunk) => {
                        body += chunk;
                    });
                    res.on('end', () => {
                        resolve(body);
                    });
        });

        req.on('error', (e) => {
            reject(e);
        });
        req.write(postDataStr);
        req.end();
    });
};

http.createServer((req, res) => {    
    if(req.url !== '/' || req.method !== 'POST'){
        res.writeHead(200, {'Content-Type': 'text/plain;charset=utf-8'});
        res.end('こんにちは');
    }

    let body = '';
    req.on('data', (chunk) => {
        body += chunk;
    });        
    req.on('end', () => {
        if(body === ''){
          console.log('bodyが空です。');
          return;
        }

        let WebhookEventObject = JSON.parse(body).events[0];
        console.log(WebhookEventObject);        
        //メッセージが送られて来た場合
        if(WebhookEventObject.type === 'message'){
            let SendMessageObject;
            if(WebhookEventObject.message.type !== 'text')return;

                let status;
                axios.get(URL)
                .then((response) => {
                    // console.log(response.data);
                    let item = response.data;
                    status = item.match(/operatinginfo"><strong>(.*?)<\/strong>/)[1];
                    console.log(status);

                    SendMessageObject = [{
                        type: 'text',
                        text: status
                    }];

                    client(WebhookEventObject.replyToken, SendMessageObject)
                    .then((body)=>{
                        console.log(body);
                    },(e)=>{console.log(e)});

                });
        }

        res.writeHead(200, {'Content-Type': 'text/plain'});
        res.end('su');
    });

}).listen(PORT);

console.log(`Server running at ${PORT}`);
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up