LoginSignup
5
4

More than 3 years have passed since last update.

ヤフオクとBuyeeをNodejsでスクレイピング

Last updated at Posted at 2018-06-27

それぞれの場合で「canon カメラ -ジャンク」のキーワードで調べみました。

1 ヤフオク

ポイント
・検索キーワードをエンコードしてはいけない(エンコードするとヤフオク側でデコードしてくれない)
・検索キーワードに「+」が入っている場合、半角スペースに置き換えなければならない
・開催中のオークションを調べたいなら "/search/search"
・過去120日間のオークションを調べたいなら "/closedsearch/closedsearch" (日付は120日しか指定不可)

scriping_yahuoku.js
  const cheerio = require("cheerio-httpcli");
  const keyword = "canon カメラ -ジャンク";
  const res = cheerio.fetch("https://auctions.yahoo.co.jp/closedsearch/closedsearch",{
    p : keyword,  
    select : "02",//並び順 落札価格の高い順 絶対に 02 のように0をつける
    istatus : 2,//新品だけで絞るなら1 中古だけなら2
    ngram : 1, //あいまい検索するなら1を指定
    ei:'UTF-8',fixed:0,auccat:0,tab_ex:'commerce',slider:0
  });

  const price = Number(res.$(".ePrice").first().text().replace(/\D/g,"")); //検索結果の一番上の落札価格

  //上記の条件でfetchした場合、直近30日の最高落札価格を取得する場合は下記のようにする
  const moment = require('moment');
  const today = moment();
  res.$(".d").each(function(i,el){
    end_day = moment(res.$(this),'MM-DD');
    if(today.diff(end_day, 'days') <= 30){
      pr3 = res.$('.ePrice').eq(i).text().replace(/\D/g,'');
      return false;
    }
  });

2 Buyee

ポイント
・検索キーワードは空白を「+」に置き換えしてからエンコードする(encodeURIでOK)
・問題無くfetchできたのにエラー吐く時がある。その際はエラーオブジェクトにcheerioオブジェクトが入っている

scriping_buyee.js
  const cheerio = require("cheerio-httpcli");
  const keyword = "canon カメラ -ジャンク";
  const res = cheerio.fetch('https://buyee.jp/item/search/query/' + encodeURI(keyword.replace(/\s/g, '+')), {
    item_status: 2, //中古
    translationType: 1,
    order: 'd',
    sort: 'end'
  }).then((result) => {
    return Promise.resolve(result);
  }, (err) => {
    if (err.$) {
      console.log('$があるからこれがcheerioオブジェクト');
      return Promise.resolve(err);
    } else return Promise.reject(err);
  });

  const date_text = res.$('.generalicon-clock').first().text();
  var p = 0;
  var p1 = 0;
  var p2 = 0;

  if (date_text) {
    const date = time.replace(/日/, ""); //検索結果の一番上のオークション残り開催日数
    try {
      data.auction_url = "https://page.auctions.yahoo.co.jp/jp/auction" + (res.$('.product_whole').first().find('a').first().attr('href').match(/\/[a-z]*\d{9,}$/)[0]); //オークションIDを抜き出してヤフオクのオークションページURLを取得する
    } catch (e) {
      data.auction_url = 'Error';
    }

    //現在価格の最高額を求める
    res.$('.product_price').each((i, el) => {
      p = Number(res.$(el).text().replace(/\(.+\)/g, '').replace(/\D/g, ''));
      if (p > pr1) pr1 = p;
    });

    //即決価格の最高額を求める
    res.$('.product_bidorbuy').each((i, el) => {
      p = Number(res.$(el).text().replace(/\(.+\)/g, '').replace(/\D/g, ''));
      if (typeof (p) == Number && p > pr2) pr2 = p;
    });
  }
5
4
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
5
4