LoginSignup
5
4

More than 5 years have passed since last update.

【すぐできる】PHPでヤフオクのスクレイピングライブラリ(非公式)を試してみる

Last updated at Posted at 2018-09-27

はじめに

比較的簡単そうなライブラリを見つけたので忘備録代わりに書きました。

下記のスクレイピングライブラリを用いてヤフオクのスクレイピングを行います。
https://github.com/notfoundsam/yahoo-auction

具体的には、コンソールアプリケーションを作成し xml を取得します。

スクレイピングには十分注意してください。
また、本記事の実行結果に対して責任を負いません。自己責任にておこなってください。

環境の準備

5.4 以上のバージョンが使える PHP の環境を用意しましょう。(本記事では割愛)

PHPに加え、下記のモジュールをインストールしておく。

  • php-curl
  • php-mbstring
  • php-xml

まずは、git からソースコードをダウンロード。

git clone https://github.com/notfoundsam/yahoo-auction.git yahoo-auction

次は、必要なライブラリを composer でインストール。

php composer.phar install

vender ディレクトリ配下にライブラリがインストールできていれば成功です。

$ ls
LICENSE  README.md  composer.json  composer.lock  composer.phar  cookie.cache  examples  src  vendor

アプリケーションキーを取得

Yahooデベロッパーネットワークからアプリケーションキーを取得する。
始め方は下記リンク
https://developer.yahoo.co.jp/start/

0. Yahooアカウントがない場合は場合は会員登録

アカウントはyahooメールなどで使うアカウントと同じです。
登録はこちらから。
https://account.edit.yahoo.co.jp/registration?.src=ydn&.done=https%3A%2F%2Fdeveloper.yahoo.co.jp%2Fstart%2F

1. アプリケーションを登録し、Clientキーを取得

スクリーンショット 2018-09-27 16.05.05.png

プログラムの実行

examples/main.php を編集

下記の変数にそれぞれ値を入れる。

$userName : Yahoo アカウントの名称(メールアドレスの@yahoo.co.jpの前部分)
$userPass : Yahoo アカウントのパスワード
$appId   : 先ほどコピーした Client ID

21, 24, 27行目をコメントアウトする。

  1 <?php
  2
  3 require __DIR__ . '/../vendor/autoload.php';
  4
  5 use Yahooauc\Browser as Browser;
  6
  7 $userName = "your_yahoo_user";
  8 $userPass = "your_yahoo_pass";
  9 $appId    = "your_app_id";
 10
 11 /* Get saved cookie */
 12 $cookie = file_get_contents('cookie.cache');
 13 $cookieJar = $cookie !== false ? unserialize($cookie) : [];
 14
 15 $browser = new Browser($userName, $userPass, $appId, $cookieJar);
 16
 17 /* Get information about lot */
 18 var_dump($browser->getAuctionInfoAsXml("lotId"));
 19
 20 /* Get list of lots from first bidding page */
 21 #var_dump($browser->getBiddingLots(1));
 22
 23 /* Get IDs of lots from first won page */
 24 #var_dump($browser->getWonIds(1));
 25
 26 /* Bid on lot */
 27 #var_dump($browser->bid("lotId", 100));
 28
 29 /* Save latest cookie */
 30 $cookieJar = $browser->getCookie();
 31 $cookie = serialize($cookieJar);
 32 file_put_contents('cookie.cache', $cookie);

実行

ヤフオクのURLからlotIdを取得。
下記のURLだと r272086302 が該当するらしい。
https://page.auctions.yahoo.co.jp/jp/auction/r272086302

18行目の lotId を書き換えて実行。
すると xml が出力される。

$ php examples/main.php
object(SimpleXMLElement)#25 (2) {
  ["@attributes"]=>
  array(3) {
    ["totalResultsAvailable"]=>
    string(1) "1"
    ["totalResultsReturned"]=>
    string(1) "1"
    ["firstResultPosition"]=>
    string(1) "1"
  }
  ["Result"]=>
  object(SimpleXMLElement)#26 (58) {
    ["AuctionID"]=>
    string(10) "r272086302"
    ["CategoryID"]=>
    string(10) "2084193587"
    ["CategoryFarm"]=>
    string(1) "3"
    ["CategoryIdPath"]=>
    string(57) "0,23336,2084039759,2084039792,23412,2084042169,2084193587"
    ["CategoryPath"]=>
    string(171) "オークション > コンピュータ > パソコン > Windows > ノートブック、ノートパソコン > パナソニック、松下電器産業 > 14インチ以上"
    ["Title"]=>
    string(114) "Panasonic ノートパソコン CF-LX6LDGQR  Let's note Let's note 2018年春モデル 全国送料無料!"
    ["Seller"]=>
    object(SimpleXMLElement)#23 (4) {
      ["Id"]=>
      string(11) "kazuag_2016"
      ["Rating"]=>
      object(SimpleXMLElement)#39 (6) {
        ["Point"]=>
        string(3) "757"
        ["TotalGoodRating"]=>
        string(3) "766"
        ["TotalNormalRating"]=>
        string(1) "2"
        ["TotalBadRating"]=>
        string(1) "9"
        ["IsSuspended"]=>
        string(5) "false"
        ["IsDeleted"]=>
        string(5) "false"
      }
      ["ItemListURL"]=>
      string(83) "https://auctions.yahooapis.jp/AuctionWebService/V2/sellingList?sellerID=kazuag_2016"
      ["RatingURL"]=>
      string(76) "https://auctions.yahooapis.jp/AuctionWebService/V1/ShowRating?id=kazuag_2016"
    }
    ["AuctionItemUrl"]=>
    string(55) "https://page.auctions.yahoo.co.jp/jp/auction/r272086302"
    ["Img"]=>
    object(SimpleXMLElement)#24 (7) {
      ["Image1"]=>
      string(161) "https://auctions.c.yimg.jp/images.auctions.yahoo.co.jp/image/dr000/auc0405/users/7f21a39bebfc9b40399c86600b230d8a8cefb62c/i-img1200x900-15271617998nogmn77692.jpg"
      ["Image2"]=>
      string(161) "https://auctions.c.yimg.jp/images.auctions.yahoo.co.jp/image/dr000/auc0405/users/7f21a39bebfc9b40399c86600b230d8a8cefb62c/i-img640x480-1527303964pbsbnu623741.jpg"
      ["Image3"]=>
      string(161) "https://auctions.c.yimg.jp/images.auctions.yahoo.co.jp/image/dr000/auc0405/users/7f21a39bebfc9b40399c86600b230d8a8cefb62c/i-img640x480-1527303968wyla78639367.jpg"
      ["Image4"]=>
      string(161) "https://auctions.c.yimg.jp/images.auctions.yahoo.co.jp/image/dr000/auc0405/users/7f21a39bebfc9b40399c86600b230d8a8cefb62c/i-img640x480-1527303972jkzblb637411.jpg"
      ["Image5"]=>
      string(161) "https://auctions.c.yimg.jp/images.auctions.yahoo.co.jp/image/dr000/auc0405/users/7f21a39bebfc9b40399c86600b230d8a8cefb62c/i-img640x480-1527303976ybvks4183322.jpg"
      ["Image6"]=>
      string(161) "https://auctions.c.yimg.jp/images.auctions.yahoo.co.jp/image/dr000/auc0405/users/7f21a39bebfc9b40399c86600b230d8a8cefb62c/i-img640x480-1527303981vlp2yu639415.jpg"
      ["Image7"]=>
      string(161) "https://auctions.c.yimg.jp/images.auctions.yahoo.co.jp/image/dr000/auc0405/users/7f21a39bebfc9b40399c86600b230d8a8cefb62c/i-img640x480-1527303985jtxeoj620661.jpg"
    }
    ["Thumbnails"]=>
    object(SimpleXMLElement)#28 (7) {
      ["Thumbnail1"]=>
      string(206) "https://wing-auctions.c.yimg.jp/sim?furl=auctions.c.yimg.jp/images.auctions.yahoo.co.jp/image/dr000/auc0405/users/7f21a39bebfc9b40399c86600b230d8a8cefb62c/i-img1200x900-15271617998nogmn77692.jpg&cp=5&rc=298"
      ["Thumbnail2"]=>
      string(206) "https://wing-auctions.c.yimg.jp/sim?furl=auctions.c.yimg.jp/images.auctions.yahoo.co.jp/image/dr000/auc0405/users/7f21a39bebfc9b40399c86600b230d8a8cefb62c/i-img640x480-1527303964pbsbnu623741.jpg&cp=5&rc=298"
      ["Thumbnail3"]=>
      string(206) "https://wing-auctions.c.yimg.jp/sim?furl=auctions.c.yimg.jp/images.auctions.yahoo.co.jp/image/dr000/auc0405/users/7f21a39bebfc9b40399c86600b230d8a8cefb62c/i-img640x480-1527303968wyla78639367.jpg&cp=5&rc=298"
      ["Thumbnail4"]=>
      string(206) "https://wing-auctions.c.yimg.jp/sim?furl=auctions.c.yimg.jp/images.auctions.yahoo.co.jp/image/dr000/auc0405/users/7f21a39bebfc9b40399c86600b230d8a8cefb62c/i-img640x480-1527303972jkzblb637411.jpg&cp=5&rc=298"
      ["Thumbnail5"]=>
      string(206) "https://wing-auctions.c.yimg.jp/sim?furl=auctions.c.yimg.jp/images.auctions.yahoo.co.jp/image/dr000/auc0405/users/7f21a39bebfc9b40399c86600b230d8a8cefb62c/i-img640x480-1527303976ybvks4183322.jpg&cp=5&rc=298"
      ["Thumbnail6"]=>
      string(206) "https://wing-auctions.c.yimg.jp/sim?furl=auctions.c.yimg.jp/images.auctions.yahoo.co.jp/image/dr000/auc0405/users/7f21a39bebfc9b40399c86600b230d8a8cefb62c/i-img640x480-1527303981vlp2yu639415.jpg&cp=5&rc=298"
      ["Thumbnail7"]=>
      string(206) "https://wing-auctions.c.yimg.jp/sim?furl=auctions.c.yimg.jp/images.auctions.yahoo.co.jp/image/dr000/auc0405/users/7f21a39bebfc9b40399c86600b230d8a8cefb62c/i-img640x480-1527303985jtxeoj620661.jpg&cp=5&rc=298"
    }
    ["Initprice"]=>
    string(4) "1.00"
    ["Price"]=>
    string(8) "61500.00"
    ["Quantity"]=>
    string(1) "1"
    ["AvailableQuantity"]=>
    string(1) "1"
    ["WatchListNum"]=>
    string(2) "25"
    ["Bids"]=>
    string(3) "183"
    ["HighestBidders"]=>
    object(SimpleXMLElement)#27 (2) {
      ["@attributes"]=>
      array(1) {
        ["totalHighestBidders"]=>
        string(1) "0"
      }
      ["IsMore"]=>
      string(5) "false"
    }
    ["ItemStatus"]=>
    object(SimpleXMLElement)#29 (2) {
      ["Condition"]=>
      string(5) "other"
      ["Comment"]=>
      string(34) "展示品ですが1年保証です"
    }
    ["ItemReturnable"]=>
    object(SimpleXMLElement)#30 (2) {
      ["Allowed"]=>
      string(5) "false"
      ["Comment"]=>
      string(75) "初期不良の場合メーカー保証にて対応をお願い致します"
    }
    ["StartTime"]=>
    string(25) "2018-09-24T15:37:37+09:00"
    ["EndTime"]=>
    string(25) "2018-09-30T22:36:48+09:00"
    ["Bidorbuy"]=>
    string(9) "178000.00"
    ["TaxRate"]=>
    string(1) "0"
    ["Reserved"]=>
    object(SimpleXMLElement)#31 (0) {
    }
    ["IsBidCreditRestrictions"]=>
    string(4) "true"
    ["IsBidderRestrictions"]=>
    string(4) "true"
    ["IsBidderRatioRestrictions"]=>
    string(4) "true"
    ["IsEarlyClosing"]=>
    string(4) "true"
    ["IsAutomaticExtension"]=>
    string(4) "true"
    ["IsOffer"]=>
    string(5) "false"
    ["HasOfferAccept"]=>
    string(5) "false"
    ["IsDsk"]=>
    string(4) "true"
    ["IsCharity"]=>
    string(5) "false"
    ["SalesContract"]=>
    string(5) "false"
    ["IsFleaMarket"]=>
    string(5) "false"
    ["Option"]=>
    object(SimpleXMLElement)#32 (4) {
      ["FreeshippingIcon"]=>
      string(70) "https://s.yimg.jp/images/auct/template/ui/auc_mod/ic_free_shipping.png"
      ["BuynowIcon"]=>
      string(61) "https://s.yimg.jp/images/auct/template/ui/auc_mod/ic_9008.gif"
      ["EasyPaymentIcon"]=>
      string(41) "https://s.yimg.jp/images/pay/icon_s16.gif"
      ["IsTradingNaviAuction"]=>
      string(4) "true"
    }
    ["Description"]=>
    object(SimpleXMLElement)#33 (0) {
    }
    ["SeoKeywords"]=>
    object(SimpleXMLElement)#34 (0) {
    }
    ["Payment"]=>
    object(SimpleXMLElement)#35 (1) {
      ["EasyPayment"]=>
      object(SimpleXMLElement)#39 (2) {
        ["IsCreditCard"]=>
        string(4) "true"
        ["IsNetBank"]=>
        string(4) "true"
      }
    }
    ["BlindBusiness"]=>
    string(10) "impossible"
    ["SevenElevenReceive"]=>
    string(10) "impossible"
    ["ChargeForShipping"]=>
    string(6) "seller"
    ["Location"]=>
    string(9) "東京都"
    ["IsWorldwide"]=>
    string(5) "false"
    ["ShipTime"]=>
    string(5) "after"
    ["ShipSchedule"]=>
    string(1) "1"
    ["IsPrivacyDeliveryAvailable"]=>
    string(5) "false"
    ["ManualStartTime"]=>
    string(25) "2018-09-24T15:37:37+09:00"
    ["Shipping"]=>
    object(SimpleXMLElement)#36 (2) {
      ["@attributes"]=>
      array(1) {
        ["totalShippingMethodAvailable"]=>
        string(1) "1"
      }
      ["Method"]=>
      object(SimpleXMLElement)#39 (5) {
        ["Index"]=>
        string(1) "0"
        ["Name"]=>
        string(15) "ゆうパック"
        ["NameRaw"]=>
        string(15) "ゆうパック"
        ["IsOfficialDelivery"]=>
        string(5) "false"
        ["IsPrivacyDeliveryAvailable"]=>
        string(5) "false"
      }
    }
    ["BaggageInfo"]=>
    object(SimpleXMLElement)#37 (4) {
      ["Size"]=>
      string(3) "-"
      ["SizeIndex"]=>
      object(SimpleXMLElement)#39 (0) {
      }
      ["Weight"]=>
      string(3) "-"
      ["WeightIndex"]=>
      object(SimpleXMLElement)#40 (0) {
      }
    }
    ["IsAdult"]=>
    string(5) "false"
    ["IsCreature"]=>
    string(5) "false"
    ["IsSpecificCategory"]=>
    string(5) "false"
    ["IsCharityCategory"]=>
    string(5) "false"
    ["CharityOption"]=>
    object(SimpleXMLElement)#38 (1) {
      ["Proportion"]=>
      string(1) "0"
    }
    ["AnsweredQAndANum"]=>
    string(1) "0"
    ["Status"]=>
    string(4) "open"
    ["ItemDescriptionUrl"]=>
    string(77) "https://page.auctions.yahoo.co.jp/jp/show/description?aID=r272086302&direct=1"
  }
}

あとは csv にするなり、 html にするなり、好きにできそう。

まとめ

PHP のヤフオクスクレイピングライブラリを試した。
ソースコードを読んでいると、落札リストの取得や、注文なんかもできそうな気がする。

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