LoginSignup
9
9

More than 3 years have passed since last update.

ebayのAPI触ってみた

Last updated at Posted at 2021-01-03

はじめに

eBay Developers Programへの登録を済ませておいてください
こちらにガイドがあるので参考にしてください

Finding APIを使用してみる

Finding API
eBayプラットフォームの商品検索機能です
eBayに出品された商品に対する検索結果をAPIで取得することができます

pythonで実行してみます

ebay_api.py
import requests
import csv

appkey = 取得したAPP KEY
keywords = 検索したいキーワード

URL = "http://svcs.ebay.com/services/search/FindingService/v1?OPERATION-NAME=findItemsByKeywords"\
    "&SERVICE-VERSION=1.0.0"\
    f"&SECURITY-APPNAME={appkey}"\
    "&RESPONSE-DATA-FORMAT=JSON"\
    "&REST-PAYLOAD"\
    f"&keywords={keywords}"


def get_page():
    request = requests.get(URL)
    products = request.json()

    for item in (products["findItemsByKeywordsResponse"][0]["searchResult"][0]["item"]):
        itemId = item["itemId"][0]
        title = item["title"][0]
        currency = item["sellingStatus"][0]["currentPrice"][0]["@currencyId"]
        price = item["sellingStatus"][0]["currentPrice"][0]["__value__"]

        data = {
            "itemId": itemId,
            "title": title,
            "currency": currency,
            "price": price
        }
        with open("eBayAPI.csv", "a") as csvfile:
            row = [
                data["itemId"],
                data["title"],
                data["currency"],
                data["price"]
            ]
            writer = csv.writer(csvfile)
            writer.writerow(row)


if __name__ == "__main__":
    get_page()

返ってくる値

def get_page():
    request = requests.get(URL)
    products = request.json()
    print(products)

ここで返ってくる値はこのような形で返ってきます。

{
  "findItemsByKeywordsResponse": [{
    "ack": ["Success"],
    "version": ["1.13.0"],
    "timestamp": ["2021-01-07T03:03:03.718Z"],
    "searchResult": [{
      "@count": "100",
      "item": [
        {
          "itemId": ["303483892785"],
          "title": ["Spider-man retro Marvel Legends Venom Miles Morales Spiderverse Gwen Stacy UPICK"],
          "globalId": ["EBAY-US"],
          "primaryCategory": [{
            "categoryId": ["158671"],
            "categoryName": ["Comic Book Heroes"]
          }],
          "galleryURL": ["https://thumbs1.ebaystatic.com/pict/04040_0.jpg"],
          "viewItemURL": ["https://www.ebay.com/itm/Spider-man-retro-Marvel-Legends-Venom-Miles-Morales-Spiderverse-Gwen-Stacy-UPICK-/303483892785?var=0"],
          "autoPay": ["true"],
          "postalCode": ["951**"],
          "location": ["San Jose,CA,USA"],
          "country": ["US"],
          "shippingInfo": [{
            "shippingServiceCost": [{
              "@currencyId": "USD",
              "__value__": "5.99"
            }],
            "shippingType": ["FlatDomesticCalculatedInternational"],
            "shipToLocations": ["Worldwide"],
            "expeditedShipping": ["false"],
            "oneDayShippingAvailable": ["false"],
            "handlingTime": ["1"]
          }],
          "sellingStatus": [{
            "currentPrice": [{
              "@currencyId": "USD",
              "__value__": "12.95"
            }],
            "convertedCurrentPrice": [{
              "@currencyId": "USD",
              "__value__": "12.95"
            }],
            "sellingState": ["Active"],
            "timeLeft": ["P5DT0H12M28S"]
          }],
          "listingInfo": [{
            "bestOfferEnabled": ["false"],
            "buyItNowAvailable": ["false"],
            "startTime": ["2020-02-12T03:15:31.000Z"],
            "endTime": ["2021-01-12T03:15:31.000Z"],
            "listingType": ["FixedPrice"],
            "gift": ["false"], "watchCount": ["502"]
          }],
          "returnsAccepted": ["false"],
          "condition": [{
            "conditionId": ["3000"],
            "conditionDisplayName": ["Used"]
          }],
          "isMultiVariationListing": ["true"],
          "topRatedListing": ["false"]
        },
        {
          "itemId": ["133473696803"]...

各自で必要な値を抜き出してください。
今回は、商品ID、商品名、通貨、値段を取得する様にしています。

        itemId = item["itemId"][0]
        title = item["title"][0]
        currency = item["sellingStatus"][0]["currentPrice"][0]["@currencyId"]
        price = item["sellingStatus"][0]["currentPrice"][0]["__value__"]

あとは取得した商品情報がcsvで出力される様にしています。

他にもフィルター検索や特定のストアで検索かけることもできるみたいなので
上手く活用していけば市場調査が楽になるかと思います。

次はebaysdkの記事について書こうと思います。

9
9
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
9
9