How to Extract Product Information via Amazon's MWS API

How to Extract Product Information via Amazon’s MWS API
Amazon’s Marketplace Web Service (MWS) API is a powerful tool that enables online sellers and developers to automate and enhance their business processes. One of its key features is the ability to extract product information. In this article, we will guide you through the process of how to get product information via Amazon’s MWS API.
Step 1: Register for Amazon MWS
Before you can use the Amazon MWS API, you’ll need to register your application with Amazon. Go to the Amazon MWS registration page, fill in the required details, and accept the Amazon MWS License Agreement. Once completed, you’ll be provided with your AWS Access Key ID
and Secret Access Key
.
Step 2: Set Up Your Environment
The Amazon MWS API is accessible via HTTP. You can use whatever language you’re comfortable with, but for this tutorial, we’ll use Python and the requests
library. Install it with:
pip install requests
Step 3: Construct Your Request
To request product information, you’ll need to use the GetMatchingProduct
operation. Here’s a basic example of how to construct this request:
import requests
import hmac
import hashlib
import base64
import time
AWS_ACCESS_KEY_ID = 'your-access-key'
AWS_SECRET_ACCESS_KEY = 'your-secret-key'
MARKETPLACE_ID = 'your-marketplace-id'
SellerId = 'your-seller-id'
ASIN = 'product-asin'
base_url = 'https://mws.amazonservices.com/Products/2011-10-01'
method = 'POST'
path = '/Products/2011-10-01'
timestamp = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime())
parameters = {
'AWSAccessKeyId': AWS_ACCESS_KEY_ID,
'Action': 'GetMatchingProduct',
'SellerId': SellerId,
'MWSAuthToken': 'AMZN.mws.your-auth-token',
'MarketplaceId': MARKETPLACE_ID,
'ASINList.ASIN.1': ASIN,
'SignatureMethod': 'HmacSHA256',
'SignatureVersion': '2',
'Timestamp': timestamp,
'Version': '2011-10-01',
}
def calculate_signature(secret_access_key, method, host, path, parameters):
sorted_params = sorted(parameters.items(), key=lambda x: x[0])
canonical_query_string = '&'.join(['%s=%s' % (k, v) for (k, v) in sorted_params])
string_to_sign = '\n'.join([method, host, path, canonical_query_string])
h = hmac.new(bytes(secret_access_key, 'utf-8'), digestmod=hashlib.sha256)
h.update(string_to_sign.encode('utf-8'))
return base64.b64encode(h.digest()).decode()
parameters['Signature'] = calculate_signature(AWS_SECRET_ACCESS_KEY, method, base_url, path, parameters)
response = requests.post(base_url, params=parameters)
This script sends a POST
request to the GetMatchingProduct
operation. It includes the necessary parameters and calculates the signature required for authentication.
Step 4: Parse the Response
The response from Amazon MWS will be in XML format. You can parse it using any XML parsing library. Here’s how you can do it using Python’s built-in xml.etree.ElementTree
:
import xml.etree.ElementTree as ET
root = ET.fromstring(response.content)
namespace = {'a': 'http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd'}
for product in root.findall('.//a:Product', namespace):
title = product.find('.//a:Title', namespace).text
brand = product.find('.//a:Brand', namespace).text
print('Title:', title, 'Brand:', brand)
This script extracts the title and brand for each product in the response.
By following these steps, you can retrieve product information via Amazon’s MWS API. Remember to handle and store your credentials securely and to respect Amazon’s usage policies. Happy coding!
About Saturn Cloud
Saturn Cloud is your all-in-one solution for data science & ML development, deployment, and data pipelines in the cloud. Spin up a notebook with 4TB of RAM, add a GPU, connect to a distributed cluster of workers, and more. Join today and get 150 hours of free compute per month.