1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

python完全初心者だけどebayのFinding APIを使ってみる

Posted at

はじめに

eBayはアメリカ発のプラットフォームであり、簡単に言えばECサイトと言って、Amazonや楽天、メルカリのようなめっちゃでかい通販サイトである。日本人の一般利用は少ないので耳なじみが無いのは無理はない(俺もこの前まで知らなかった)が、海外への販路開拓に向けてデータを収集する段階で海外通販に詳しい人から「海外はやっぱりebayでしょ~」と言われたので今回はデータを集めるという大義の元に初めてPythonに触れて少し勉強してみる。

したこと

カテゴリやキーワードでsortして数千件のデータをCSV形式で保存する。
データを集めたらあとは煮るなり焼くなりで、自分はGoogleSpreadSheetで開いてグラフにして資料の一部にした。
B列にimage()を入れて見やすくしているがこのCSVはただのソースとしか使わなかったのであんまり意味なかったかも。
eBayjapaneseknifeAPI_with_category_and_price_filter - Google スプレッドシート - Google Chrome 2024-05-13 20-54-35.gif

下準備

APIやeBay Developers Programについては他にいっぱいこと優秀な資料があるからそこを見るとよい。参考にしたサイトやページを張っておく。

(ebay公式API導入ガイド)

(Developers Program登録までの流れ)

変な矢印になっちゃったけどこのAppIDまで取得できれば準備OK。
上のサイトでも書かれていたと思うけど、アカウント登録してから数日待たないといけないからそこは注意。(私の場合は夜中思いついて登録して次の日の昼くらいにはできるようになっていた。)
スクリーンショット 2024-05-13 212323 (1).png

script

とりあえずこれが公式のドキュメントに書いてあったエンドポイントらしい。
なぜかキーワードがハリーポッターになっている。
テストでYourAppIDの部分を入れてみて、まずは表示されるかを試そう。

http://svcs.ebay.com/services/search/FindingService/v1?OPERATION-NAME=findItemsByKeywords
   &SERVICE-VERSION=1.0.0
   &SECURITY-APPNAME=YourAppID
   &RESPONSE-DATA-FORMAT=JSON
   &REST-PAYLOAD
   &keywords=harry%20potter%20phoenix 

※pythonの勉強で少し触れてみたかったというのが大きな理由の為コーデイングはchatGPTに手伝ってもらっている。

import.py
import requests
import csv

keyword = "" #ここに検索したいキーワードを入れる
app_name = ""  # 自分のAppIDに置き換えてください
entries_per_page = 100  # 1ページあたりの最大結果数
category_id = "20649"  # カテゴリーID(例として20649 はキッチン用品カテゴリーのID)
min_price = ""  # 最小価格(ドル単位)
max_price = ""  # 最大価格(ドル単位)

URL = f"https://svcs.ebay.com/services/search/FindingService/v1"
URL += "?OPERATION-NAME=findItemsByKeywords"
URL += f"&SERVICE-VERSION=1.0.0&SECURITY-APPNAME={app_name}"
URL += "&RESPONSE-DATA-FORMAT=JSON&REST-PAYLOAD"
URL += f"&keywords={keyword}&paginationInput.entriesPerPage={entries_per_page}"
URL += f"&categoryId={category_id}"  # カテゴリーIDをURLに追加します
URL += f"&itemFilter(0).name=MinPrice&itemFilter(0).value={min_price}&itemFilter(0).paramName=Currency&itemFilter(0).paramValue=USD"
URL += f"&itemFilter(1).name=MaxPrice&itemFilter(1).value={max_price}&itemFilter(1).paramName=Currency&itemFilter(1).paramValue=USD"

def get_page(page_number):
    request_url = f"{URL}&paginationInput.pageNumber={page_number}"
    request = requests.get(request_url)
    products_data = request.json()

    with open("#CSVファイル名", "a", encoding="utf-8", newline="") as csvfile:
        writer = csv.writer(csvfile)

        for item in products_data.get("findItemsByKeywordsResponse", [{}])[0].get("searchResult", [{}])[0].get("item", []):
            item_id = item.get("itemId", [""])[0]
            title = item.get("title", [""])[0]
            currency = item.get("sellingStatus", [{}])[0].get("currentPrice", [{}])[0].get("@currencyId", "")
            price = item.get("sellingStatus", [{}])[0].get("currentPrice", [{}])[0].get("__value__", "")
            item_url = item.get("viewItemURL", [""])[0]

            product_id = item.get("productId", [{}])[0].get("__value__", "")
            gallery_url = item.get("galleryURL", [""])[0]
            location = item.get("location", [""])[0]
            condition = item.get("condition", [{}])[0].get("conditionDisplayName", [""])[0]
            description = item.get("description", [""])[0]

            row = [item_id, title, currency, price, item_url, product_id, gallery_url, location, condition, description]
            writer.writerow(row)


if __name__ == "__main__":
    # 取得するページ数を指定します
    page_count = 30  # 例:10ページを取得します(1ページあたり100件のアイテム)
    
    for page_number in range(1, page_count + 1):
        get_page(page_number)

商品IDや商品タイトル、値段なんかはこれでひっぱてこれる。
件数はページ数で調整して制限食らわない程度に好きな数を引っ張て来ればよいのではないだろうか。

おわりに

今回は初めてPythonとebayのAPIに触れてみた。
一撃で越境ECのデータを集められるのは中小企業のマーケターや社員の資料製作には役立つし、海外販路開拓の助けにもなるだろう。より多くのデータは他のAPIを使って取得できるのだが今回はとりあえず触ってみるだけで、できればdescriptionも欲しかった。まあ、しょうがないね。

便利なんだけどJSフレームワークを使ってウェブサイトを作ってる方が好きだなぁ...

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?