LoginSignup
0
2

More than 5 years have passed since last update.

Nintendo Switchを買いたくてNode.jsでアマゾンを監視してみた話。

Last updated at Posted at 2017-08-18

当方はNodeを三日間ぐらい学んでる初心者で、試みにNode.jsで監視プログラムを作ってみた。
約10回ぐらいページの値段を獲得することはできたが、その後獲得できなくなって、おそらくブロック
クローラ対策かなっと考えられる。
請求の回数を減らして、また他の方案を考えるとさすがに知識不足なので止めた。
結局、運がよくアマゾンを閲覧しているところ、突然入荷で無事購入。

Node.js
var express=require('express');
var superagent=require('superagent');
var cheerio=require('cheerio');
var schedule = require('node-schedule');


var app=express();
app.listen(3000,function (req,res) {
        /* body... */
        console.log('app is running at port 3000');
    })

function getPrice() {
    app.get('/',function (req,res,next) {
    /* body... */

    superagent.get('https://www.amazon.co.jp/%E4%BB%BB%E5%A4%A9%E5%A0%82-Nintendo-Switch-%E3%82%B9%E3%83%97%E3%83%A9%E3%83%88%E3%82%A5%E3%83%BC%E3%83%B32%E3%82%BB%E3%83%83%E3%83%88/dp/B072FGP433/ref=sr_1_10?s=videogames&ie=UTF8&qid=1502075389&sr=1-10&keywords=Nintendo+Switch')
        .end(function (err,sres) {
            /* body... */
            if(err){
                return next(err);
            }


            var $=cheerio.load(sres.text);
            var items=[];
            $('#priceblock_ourprice').each(function (idex,element) {
                /* body... */
                var $element=$(element);
                items.push($element.text())
            });

            res.send(items);
            var array=items[0].split("");
            var p=Number.parseInt(array[array.length-6]);
            if(p===3){
                $('#a-autoid-1').trigger("click");
            }else {
                console.log("do not buy");
            }
            console.log('success');
            console.log(p);
            return p;
        });
    }); 


}

var rule = new schedule.RecurrenceRule();
rule.second=10;
var j = schedule.scheduleJob(rule, function(){

  console.log('getprice is running');
  console.log('scheduleCronstyle:' + new Date());
  var pri=getPrice();
  console.log(pri);
});
0
2
0

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
0
2