Python スクレイピング | YahooShoppingAPIのレスポンスから商品情報抽出【後編】

Shopping

参考ページ

https://developer.yahoo.co.jp/webapi/shopping/shopping/v3/itemsearch.html

解説動画

Python スクレイピング | YahooShoppingAPIのレスポンスからデータ抽出【前編】
【Note】もくじ00:00 - 前回までのあらすじ00:50 - 検索結果03:51 - request07:16 - hitsコード詳細はこちらから↓ブログ↓===============...

スクリプト

import json
import pprint
import configparser
from pathlib import Path
import requests

BASE_DIR = Path(__file__).resolve().parent.parent
config_ini = configparser.ConfigParser()
config_ini.read(BASE_DIR / "setting" / "config.ini", encoding='utf-8')
APP_ID = config_ini['YAHOO_SHOPPING_SETTING']['ID'] #ID
yahooShoppingURL = "https://shopping.yahooapis.jp/ShoppingWebService/V3/itemSearch" #API REQUEST 

# パラメーター
params = {"appid":APP_ID,
          "query":"NC-A57-K", # UTF-8エンコードされた検索キーワード
        # "jan_code": "4549980191279", # JAN_CODE パラメータ
        "image_size":600, #画像サイズ
        # "price_from": 10000, #下限 10000円
        "price_to":20000, #上限 20000円
        "in_stock":True, #在庫なし
        "shipping":"free", # free - 送料無料, conditional_free - 条件付き送料無料
        "payment":"1", # payment - credit card
        "delivery_area":"47", # delivery_area - きょうつく、あすつく , 13 - 東京
        "sort" : "-review_count", # -review_count - 商品レビュー数順
        "condition":"new"
        }

def search():
    res = requests.get(yahooShoppingURL,params).json()
    return res

# レスポンス

yahoo_shopping_API_res = search()
pprint.pprint(yahoo_shopping_API_res)
# 総ヒット件数 - integer
totalResultsAvailable = yahoo_shopping_API_res["totalResultsAvailable"]
print("[INFO] totalResultsAvailable", totalResultsAvailable)
# 返却された商品件数
totalResultsReturned = yahoo_shopping_API_res["totalResultsReturned"]
print("[INFO] totalResultsReturned", totalResultsReturned)
# 検索クエリ request/query
query = yahoo_shopping_API_res["request"]["query"]
print("[INFO] query", query)
# ループ
for index in range(totalResultsReturned):
    # 商品名 hits/name	、商品説明 hits/description、在庫の有無 hits/inStock	
    name = yahoo_shopping_API_res["hits"][index]["name"]
    description = yahoo_shopping_API_res["hits"][index]["description"]
    inStock = yahoo_shopping_API_res["hits"][index]["inStock"]
    print("[INFO] name", name)
    print("[INFO] description", description)
    if inStock is True:
        print("[INFO] inStock", "在庫あり")
    else:
        print("[ERROR] 在庫なし")
    # 商品URL hits/url, 商品コード hits/code, 商品の状態 hits/condition	, 税抜きの価格 hits/taxExcludePrice	
    url = yahoo_shopping_API_res["hits"][index]["url"]
    code = yahoo_shopping_API_res["hits"][index]["code"]
    condition = yahoo_shopping_API_res["hits"][index]["condition"]
    print("[INFO] url", url)
    print("[INFO] code", code)
    print("[INFO] condition", condition)
    # プレミアム会員価格 - premiumPrice
    primium_price = yahoo_shopping_API_res["hits"][index]["premiumPrice"]
    print("[INFO] primium_price", primium_price)
    # 画像について
    imageId = yahoo_shopping_API_res["hits"][index]["imageId"]
    print("[INFO] imageId", imageId)
    image_medium = yahoo_shopping_API_res["hits"][index]["image"]["medium"]
    print("[INFO] image_medium", image_medium)
    exImage = yahoo_shopping_API_res["hits"][index]["exImage"]
    print("[INFO] exImage", exImage)
    # レビューについて
    review = yahoo_shopping_API_res["hits"][index]["review"]["rate"]
    print("[INFO] review_rate", review)
    review_count = yahoo_shopping_API_res["hits"][index]["review"]["count"]
    print("[INFO] review_count", review_count)
    review_url = yahoo_shopping_API_res["hits"][index]["review"]["url"]
    print("[INFO] review_url", review_url)
    # アフィリエイト料率
    affiliateRate = yahoo_shopping_API_res["hits"][index]["affiliateRate"]
    print("[INFO] affiliateRate", affiliateRate)
    # 通常価格 hits/priceLabel/defaultPrice 、プレミアム会員価格 hits/priceLabel/premiumPrice
    defaultPrice = yahoo_shopping_API_res["hits"][index]["priceLabel"]["defaultPrice"]
    print("[INFO] defaultPrice", defaultPrice)
    premiumPrice = yahoo_shopping_API_res["hits"][index]["priceLabel"]["premiumPrice"]
    print("[INFO] premiumPrice", premiumPrice, "type :", type(premiumPrice))
    # 発送 hits/shipping
    shipping_name = yahoo_shopping_API_res["hits"][index]["shipping"]["name"]
    print("[INFO] shipping_name", shipping_name)
    shipping_code = yahoo_shopping_API_res["hits"][index]["shipping"]["code"]
    print("[INFO] shipping_code", shipping_code)
    # JANコード
    janCode = yahoo_shopping_API_res["hits"][index]["janCode"]
    print("[INFO] janCode", janCode)
    # ストア情報
    store_name = yahoo_shopping_API_res["hits"][index]["seller"]["name"]
    print("[INFO] store_name", store_name)
    store_url = yahoo_shopping_API_res["hits"][index]["seller"]["url"]
    print("[INFO] store_url", store_url)
    store_review_count = yahoo_shopping_API_res["hits"][index]["seller"]["review"]["count"]
    print("[INFO] store_review_count", store_review_count)
    # 配達 hits/delivery
    delivery = yahoo_shopping_API_res["hits"][index]["delivery"]
    print("[INFO] delivery", delivery)

コメント

タイトルとURLをコピーしました