SSブログ

amazon Product Advertising APIでASINをJANに [python]


スポンサードリンク
すやねむカモミール
先日、amazon MWS で ASINをJANに変換しようとして
うまくいかなかったと書いたが、

それで、結局、どうしたかというと
昔からある amazon Product Advertising APIを使って変換した

これを使うためのスクリプトは、何年か前に書いたのだが
仕様が変わっているかと思い、再度、参考サイト

AjaxTower
http://www.ajaxtower.jp/

を見に行ったが、特に更新はされていなかった

アマゾンが提供している参考ツール Signed Requests Helper
http://associates-amazon.s3.amazonaws.com/signed-requests/helper/index.html
を見ると、AjaxTowerのものとバージョンが違うのだが
それは数年前にスクリプトを書いたときにも違っていた

以前書いたスクリプトも、一応動いたのだが、
ついでに商品名や定価なども取得できるようにしたかったので
全面的に書き直すことにした

まず、ネットでライブラリを探す。

以前は python-amazon-product-api を使ったのだが
bottlennose というのがあったので、これを使ってみる

ここを参考にした
http://d.hatena.ne.jp/yuheiomori0718/20120416/1334583174

あと、以前スクリプトを書いたときには一度の問合せで複数のASINが扱えるとは知らなかったので
毎回、一個のASINしか処理できず、処理に時間がかかっていたが、

MWSが一度で10個処理できたので、もしかしたら amazon Product Advertising API も
できるかもしれないと思い、ネットで情報を拾うと、
やはり、できるようなので、今回は、10個ずつ問い合わせるようにした

もっとも、今回、AjaxTowerを再度見なおしたら、わざわざ項目まで設けて説明してあった

複数のIDをまとめて検索
http://www.ajaxtower.jp/ecs/itemlookup/index3.html

単なる見落としである

と、いうことで、以下のようなスクリプトを作成した。

やはり、ライブラリをそのまま使えるのは簡単で良い


# -*- coding: utf-8 -*-
from lxml import etree
import codecs
import sys
import re
import time
from bottlenose import api

reload(sys)
sys.setdefaultencoding("utf-8")

f1 = open("asin_list.txt")
lines = f1.readlines()
f1.close()


asin_list = []
for item in lines:
    if item != "":
        asin_list.append(item.decode('utf-8').strip())
if "" in asin_list:
    asin_list.remove("")

AMAZON_ACCESS_KEY_ID='************'
AMAZON_SECRET_KEY='********************************'
AMAZON_ASSOC_TAG='********-22'

amazon = api.Amazon(AMAZON_ACCESS_KEY_ID, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG, Region="JP")

while len(asin_list) > 0:
    query = ""
    if len(asin_list) >10:
        limit = 10
    else:
        limit = len(asin_list)

    for i in range(limit):
        query = query+asin_list.pop(0)+","
    query.strip(",")
    res = amazon.ItemLookup(ItemId=query, ResponseGroup='Large')

    root = etree.fromstring(res, etree.HTMLParser())

    items = root.xpath('//item')

    for item in items:
        asin_code = item.find('asin').text

        jan = ""
        jan = item.find('itemattributes/ean')
        if jan != None:
            jan = jan.text
        price =""
        price = item.find('offersummary/lowestnewprice/formattedprice')
        if price != None:
            price = price.text.replace('¥',"").replace(",","")
        title = item.find('itemattributes/title').text
        print ("{0}|{1}|{2}|{3}".format(asin_code,jan,price,title))

    time.sleep(6)





スポンサードリンク



nice!(0)  コメント(1)  トラックバック(0) 

nice! 0

コメント 1

Stepfougs

Viagra Overnight Shipping Fedex <a href=https://apcialisle.com/#>Cialis</a> CytotecР’В® Prix Pharmacie <a href=https://apcialisle.com/#>Cialis</a> Cialis Forum Statistiques
by Stepfougs (2020-03-24 15:04) 

コメントを書く

お名前:
URL:
コメント:
画像認証:
下の画像に表示されている文字を入力してください。

トラックバック 0

トラックバックの受付は締め切りました

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。