LoginSignup
0
2

Python Yahoo!ショッピングのAPIで検索上位調査

Last updated at Posted at 2022-09-10

Python Yahoo!ショッピングのAPIで検索上位調査

Yahooショッピングが提供しているAPIを利用して、ある検索ワードでの検索結果の上位1000件の商品の情報を取得する。下記がそのプログラムである。下記リンクにAPIの詳細が記載してあるが、制約として短時間でアクセスした場合はしばらく使えなくなることと、検索数は最大1000件までとなっている。

auto_yahooshop.py
import sys
import os
import numpy as np
import requests
import json
import time

item_name = 'amazon'
appid = '******'

### 特定ワードで検索した結果上位1000件をcsvへ吐き出す
datray1 = np.array([])
datray1  = np.append(datray1 , "商品名,価格,URL,販売店,販売店のURL")
seller_id_list = []

for k in range(10):
    url = 'https://shopping.yahooapis.jp/ShoppingWebService/V3/itemSearch?appid={}&query={}&start={}&results=100'.format(appid, item_name, k*99 + 1)
    print(url)
    call = requests.get(url)
    res_dict = json.loads(call.content)
    print('検索結果: ',res_dict['totalResultsAvailable'])
    print('検索数: ',res_dict['totalResultsReturned'])
    print('順位位置: ',res_dict['firstResultsPosition'])
    count = len(res_dict['hits'])
    for i in range(count):
        name = res_dict['hits'][i]['name']
        price = res_dict['hits'][i]['price']
        url = res_dict['hits'][i]['url']
        seller = res_dict['hits'][i]['seller']
        seller_id = seller['sellerId']
        seller_id_list.append(seller_id)
        seller_name = seller['name']
        seller_url = seller['url']
        datray1  = np.append(datray1 , '%s,%s,%s,%s,%s'%(name, price, url, seller_name, seller_url))

DAT1 =  np.column_stack(datray1)
outname1 = './sample1.csv'
np.savetxt(outname1, DAT1 , delimiter=",\n", fmt="%s", encoding='utf8')

### 特定の販売店の商品の上位1000件を吐き出す
datray2 = np.array([])
datray2  = np.append(datray2 , "商品名,価格,販売店のURL")
seller_id_list = list(set(seller_id_list))
print(seller_id_list)

for i in range(len(seller_id_list)):
    for k in range(10):
        seller_id = seller_id_list[i]
        store_url = 'https://shopping.yahooapis.jp/ShoppingWebService/V3/itemSearch?appid={}&seller_id={}&start={}&results=100'.format(appid, seller_id, k*99 + 1)
        store_call = requests.get(store_url)
        store_dict = json.loads(store_call.content)
        store_count = len(store_dict['hits'])
        for j in range(store_count):
            name = store_dict['hits'][j]['name']
            price = store_dict['hits'][j]['price']
            url = store_dict['hits'][j]['url']
            datray2  = np.append(datray2 , '%s,%s,%s'%(name.replace(',', ''), price, seller_url))
        time.sleep(1)

DAT2 =  np.column_stack(datray2)
outname2 = './sample2.csv'
np.savetxt(outname2, DAT2 , delimiter=",\n", fmt="%s")

まとめ

今回はYahoo ショッピングのAPIを利用して検索上位1000件を取得した。

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