[Python] Get product information such as ASIN and JAN with Amazon PA-API ver5.0

Introduction

Amazon PA-API (Product Advertising API) has been redesigned from version 4.0 to 5.0 from December 2019, so the script has also been reorganized for version 5.0. It was. (Need to reassemble)

Development environment

Things to prepare in advance

After creating an Amazon seller account, go to here

access key secret key partner tag

Please get.

Please install amazon-paapi5 with the pip command etc.

Scratchpad You can check the specifications of PA-API5.0 at here. With Scratchpad, you can easily enter the API with the three KEYs specified above, the input parameters and their values in the text box. You can hit. At that time, you can check the API request actually generated and the JSON response to it together. It also provides sample request codes such as Python and PHP. It's very easy to use, so it's a good idea to give it a try before you start writing code.

Sample code

Sample 1

input output
Search word ASIN
Product price
Product URL
Product title

It can also be a JAN code search word.

python


from amazon.paapi import AmazonAPI

KEY    = "<INPUT YOUR KEY>"
SECRET = "<INPUT YOUR SECRET KEY>"
TAG    = "<INPUT YOUR PARTNER TAG>"
COUNTRY = "JP"
keyword = "Nintendo Switch"

amazon = AmazonAPI(KEY, SECRET, TAG, COUNTRY)

products = amazon.search_items(keywords=keyword)
asin     = products["data"][0].asin
price    = products["data"][0].offers.listings[0].price.amount
url      = products["data"][0].detail_page_url
title    = products["data"][0].item_info.title.display_value

print(asin, price, url, title)

Sample 2

input output
ASIN ASIN
Product price
JAN code
Product URL
Product title

The .get_items () method can get an API response by inputting up to 10 ASINs in one request.


#coding:utf-8
from amazon.paapi import AmazonAPI

KEY    = "<INPUT YOUR KEY>"
SECRET = "<INPUT YOUR SECRET KEY>"
TAG    = "<INPUT YOUR PARTNER TAG>"
COUNTRY = "JP"
asin_list = ["B087QZ1FWZ","B087QW57NZ","B081T9VJS4","B087QZGN24"]#You can hit up to 10 at a time

amazon = AmazonAPI(KEY, SECRET, TAG, COUNTRY)

products = amazon.get_items(item_id_type="ASIN", item_ids=asin_list)
    #Extract information associated with each ASIN
    for asin in asin_list:
        try:
            asin  = products["data"][asin].asin
            price = int(products["data"][asin].offers.listings[0].price.amount)
            jan   = products["data"][asin].item_info.external_ids.ea_ns.display_values[0]
            url   = products["data"][asin].detail_page_url
            title = products["data"][asin].item_info.title.display_value
            print(asin, price, jan, url, title)
            
        except:
            None

Click here for the version with asin.txt as shown below

asin.txt


B087QZ1FWZ
B087QW57NZ
B081T9VJS4
B087QZGN24
B081T9Z4KG
B084XH5NW1
B07YNPWP5M
B086ZGFKPR
B086ZFTHY2
B084DF682G
B07QWR3KDW
B07MR6YMXC
B081T9MCG7
B084HPJWK9
B087D2NW77

python


#coding:utf-8
from amazon.paapi import AmazonAPI
from time import sleep

KEY    = "<INPUT YOUR KEY>"
SECRET = "<INPUT YOUR SECRET KEY>"
TAG    = "<INPUT YOUR PARTNER TAG>"
COUNTRY = "JP"
amazon = AmazonAPI(KEY, SECRET, TAG, COUNTRY)

def send_request(asin,b):
    products = amazon.get_items(item_id_type="ASIN", item_ids=asin_list)
    num = 0
    for asin in asin_list:
        try:
            num += 1
            asin  = products["data"][asin].asin
            price = products["data"][asin].offers.listings[0].price.amount
            jan   = products["data"][asin].item_info.external_ids.ea_ns.display_values[0]
            url         = products["data"][asin].detail_page_url
            monourl     = "https://mnrate.com/item/aid/"+asin
            title       = products["data"][asin].item_info.title.display_value
            print (asin, jan, price)
            b.write(asin+","+jan+","+title+","+price+","+url+","+monourl+"\n")
            sleep(0.1)
        except:
            None

#Calculate how many API requests are needed
pages = sum([1 for _ in open('asin.txt')])/10 + 1
a = open("asin.txt","r")#Input file
b = open("jan.csv","w")#Output file
asin_list = []
num = 0

for i in a:
    num += 1
    asin = i.rstrip()
    asin_list.append(asin)

    #Hit the API when the ASIN list reaches 10 length
    if len(asin_list) == 10:
        send_request(asin_list,b)
        asin_list = []

    #Last request
    elif num == pages:
        send_request(asin_list,b)

a.close()
b.close()

monourlYou can check the selling condition of the product on the url of the monorate page. By the way, this code is a chord made for chords.

Recommended Posts

[Python] Get product information such as ASIN and JAN with Amazon PA-API ver5.0
[Python] Get user information and article information with Qiita API
Get Alembic information with Python
Get information such as GPU usage from Python (nvidia-smi command)
[Python x Zapier] Get alert information and notify with Slack
Get weather information with Python & scraping
[Python] Get Python package information with PyPI API
Read JSON with Python and output as CSV
Get git branch name and tag name with python
Get CPU information of Raspberry Pi with Python
Get Gmail subject and body with Python and Gmail API
Python script to get note information with REAPER
Get media timeline images and videos with Python + Tweepy
Get comments on youtube Live with [python] and [pytchat]!
Get mail from Gmail and label it with Python3
Get date with python
Compare HTTP GET / POST with cURL (command) and Python (programming)
Try Amazon Simple Workflow Service (SWF) with Python and boto3
Get only the Python version (such as 2.7.5) on the CentOS 7 shell
Implemented LSTM derivatives such as MGU and SGU with TensorFlow
Get rid of dirty data with Python and regular expressions
Data cleansing 1 Convenient Python notation such as lambda and map
Collecting information from Twitter with Python (MySQL and Python work together)
Sample of HTTP GET and JSON parsing with python of pepper
Get product name and lowest price using Amazon Product Advertising API
Get additional data to LDAP with python (Writer and Reader)
Try Juniper JUNOS PyEz (python library) Memo 2 ~ Get information with PyEz ~
Automation of a research on geographical information such as store network using Python and Web API
Note installing modules such as pytorch and opencv with pip in Blender python (2.82a or later)