所以我试图使用他们的API从亚马逊收集评论。不幸的是,虽然看起来我可能在我的程序中的某些方面做错了。它正在发回许多其他人显然正在回复的回复。相信我,我已经看完了,每个人都有问题,没有任何效果。请帮帮我。
以下是代码:
__author__ = 'dperkins'
import requests
import amazonproduct
import time
import datetime
import hmac
import hashlib
import base64
import urllib
import ssl
from bs4 import BeautifulSoup
# Configuration for the AWS credentials
config = {
'access_key': 'XXXXXXXXXXXXXXXXXXXX',
'secret_key': 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
'associate_tag': 'dperkgithu-20',
'locale': 'us'
}
api = amazonproduct.API(cfg=config)
productASIN = ''
productTitle = ''
# Product look up for the official iPhone 5s White 16gb Unlocked
for product in api.item_search('Electronics', Keywords='iPhone'):
if product .ASIN == 'B00F3J4E5U':
productTitle = product.ItemAttributes.Title
productASIN = product.ASIN
# Product Title with ASIN and a formatted underline
print productTitle + ': ' + productASIN
underline = ''
for int in range(len(productTitle + ': ' + productASIN)):
underline += '-'
print underline
# URL First portion of the request for reviews
signatureTime = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
signatureTime = urllib.quote_plus(signatureTime) # Must url encode the timestamp
url = "http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&Operation=ItemLookup&ResponseGroup=Reviews&IdType=ASIN&ItemId=%s&AssociateTag=%s&AWSAccessKeyId=%s&Timestamp=%s" % (productASIN, api.associate_tag, api.access_key, signatureTime)
# # HMAC with SHA256 hash algorithm
# dig = hmac.new(api.secret_key, msg=url, digestmod=hashlib.sha256).digest()
# signature = base64.b64encode(dig).decode() # py3k-mode
#url = 'http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&Operation=ItemLookup&ResponseGroup=Reviews&IdType=ASIN&ItemId=%s&AssociateTag=%s&AWSAccessKeyId=%s&Timestamp=%s&Signature=%s' % (productASIN, api.associate_tag, api.access_key, signatureTime, signature)
#Split and byte order the request (poorly but should always be the same)
parameters = [1, 2, 3, 4, 5, 6, 7]
for line in url.split('&'):
if (line.startswith('AssociateTag')):
parameters[0] = line
elif (line.startswith('AWSAccessKeyId')):
parameters[1] = line
elif (line.startswith('IdType')):
parameters[2] = line
elif (line.startswith('ItemId')):
parameters[3] = line
elif (line.startswith('Operation')):
parameters[4] = line
elif (line.startswith('ResponseGroup')):
parameters[5] = line
elif (line.startswith('Timestamp')):
parameters[6] = line
rejoined = ''
i = 1
for line in parameters:
if i < len(parameters):
rejoined += line + '&'
else:
rejoined += line
i += 1
print 'Rejoined: ' + rejoined
# Prepend the request beginning
prepend = 'GET\nwebservices.amazon.com\n/onca/xml\n' + rejoined
print 'Prepend: ' + prepend
# HMAC with SHA256 hash algorithm
dig = hmac.new(api.access_key, msg=prepend, digestmod=hashlib.sha256).digest()
signature = base64.b64encode(dig).decode() # py3k-mode
print 'Signature: ' + signature
encodedSignature = urllib.quote_plus(signature) # encode the signature
print 'Encoded Signature: ' + encodedSignature
finalRequest = 'http://webservices.amazon.com/onca/xml?' + rejoined + '&Signature=' + encodedSignature
# Final request to send
print 'URL: ' + finalRequest
# Use BeautifulSoup to create the html
r = requests.get(finalRequest)
soup = BeautifulSoup(r.content)
print soup.prettify()
以下是回复:
Apple iPhone 5s, Gold 16GB (Unlocked): B00F3J4E5U
-------------------------------------------------
Rejoined: AssociateTag=dperkgithu-20&AWSAccessKeyId=XXXXXXXXXXXXXXXXXXXX&IdType=ASIN&ItemId=B00F3J4E5U&Operation=ItemLookup&ResponseGroup=Reviews&Timestamp=2014-10-01T19%3A36%3A41Z
Prepend: GET
webservices.amazon.com
/onca/xml
AssociateTag=dperkgithu-20&AWSAccessKeyId=XXXXXXXXXXXXXXXXXXXX&IdType=ASIN&ItemId=B00F3J4E5U&Operation=ItemLookup&ResponseGroup=Reviews&Timestamp=2014-10-01T19%3A36%3A41Z
Signature: YAeIaDuigxbTX7AoZzRreZzn//RbIucCiwsG9VqMayQ=
Encoded Signature: YAeIaDuigxbTX7AoZzRreZzn%2F%2FRbIucCiwsG9VqMayQ%3D
URL: http://webservices.amazon.com/onca/xml?AssociateTag=dperkgithu-20&AWSAccessKeyId=XXXXXXXXXXXXXXXXXXXX&IdType=ASIN&ItemId=B00F3J4E5U&Operation=ItemLookup&ResponseGroup=Reviews&Timestamp=2014-10-01T19%3A36%3A41Z&Signature=YAeIaDuigxbTX7AoZzRreZzn%2F%2FRbIucCiwsG9VqMayQ%3D
<html>
<body>
<itemlookuperrorresponse xmlns="http://ecs.amazonaws.com/doc/2005-10-05/">
<error>
<code>
SignatureDoesNotMatch
</code>
<message>
The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.
</message>
</error>
<requestid>
c159b688-9b08-4cc9-94fe-35245aa69cc9
</requestid>
</itemlookuperrorresponse>
</body>
</html>
答案 0 :(得分:1)
您已经成功申请亚马逊产品广告API。如果要查看返回的XML,请使用产品对象。
至于您的评论,它们不再通过API以纯文本形式提供。您需要直接从Amazon.com抓取HTML。