LoginSignup
11
13

More than 5 years have passed since last update.

楽天ランキングAPIを叩いて任意のカテゴリーの順位をCSVで保存する

Posted at

楽天ランキングAPIを叩き、
CSVに順位・商品名・URL・価格を保存するスクリプト。

任意のカテゴリIDを入力することで、
取得したいカテゴリランキングを取得可能です。
何らかの楽天ランキングに基づくデータを取りたいときにも使えるんじゃないかとか。

rakutenrank.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import csv
import sys, codecs
sys.stdout = codecs.getwriter("utf-8")(sys.stdout)

gen_input = input("catID input here: ")

url = "https://app.rakuten.co.jp/services/api/IchibaItem/Ranking/20120927?"

st_load = {
    "genreId": gen_input,
    "applicationId": ******************************,
    }

r = requests.get(url, params=st_load)

res = r.json()

f = open("raku.csv" , "ab")
writer = csv.writer(f)
writer.writerow(["No", "Itemname", "URL", "Price"])

for i in res["Items"]:
    item = i["Item"]
    Rank = item["rank"]
    Name = (item["itemName"].encode("utf-8"))
    Url = item["itemUrl"]
    Price = item["itemPrice"]

    writer.writerow([Rank, Name, Url, Price])

f.close()
11
13
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
11
13