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)
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 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()
monourl
You 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