Pythonã§Amazon Product Advertising APIã使ã
以åãAmazon Web Serviceã§æ¸ç±æ å ±ãåå¾ããï¼2008/12/20ï¼ã¨ããã¨ã³ããªãæ¸ãã¾ãããã2009å¹´8æ15æ¥ããæ°ããèªè¨¼ãå°å ¥ããã¦ãã®ã¾ã¾ã§ã¯ä½¿ããªããªã£ã¦ãããã¨ã«æ°ãã¤ãã¾ããããµã¼ãã¹åãProduct Advertising APIã«å¤æ´ãããããã§ããAmazonã®Webãµã¼ãã¹ã¯ã¨ãã©ã使ããããªãã®ã§Pythonãã使ãæ¹æ³ãæ¹ãã¦èª¿ã¹ã¦ã¿ã¾ããã以ä¸ã®ãµã¤ããåèã«ããã¦ããã ãã¾ããã
- Product Advertising API
- アマゾンAPIを使うのに2009年8月15日から認証が必要になるらしい
- AmazonアソシエイトWebサービス改めProduct Advertising APIの電子署名について調べてみました
- Python + Amazon = Pyzon
amazon.py
ã¾ããPyzonãåèã«ã¢ã¤ãã IDï¼ASINãISBNãªã©ï¼ãå ã«ååã®è©³ç´°æ å ±ãåå¾ãããããã¼ã¯ã¼ãããã¨ã«ååãæ¤ç´¢ããããã®åºæ¬çãªAmazonã¯ã©ã¹ãä½æãPyzonã®ãµãã»ããã¿ãããªæãã§ããã¡ããã¨ãèªè¨¼ç¨ã®ã·ã°ããã£ãçæãã¦ãRESTã®ãªã¯ã¨ã¹ãURLã«è¿½å ãã¦ãã¾ãã
#coding:utf-8 import urllib2 import hashlib, hmac import base64 import time """ Amazon Product Advertising APIã使ã£ã¦ååæ å ±ãåå¾ãã """ class Amazon: def __init__(self, access_key, secret_access_key, associate_tag=None): """ã³ã³ã¹ãã©ã¯ã¿""" self.amazonurl = "http://webservices.amazon.co.jp/onca/xml" self.proxy_host = None self.proxy_port = None self.access_key = access_key self.secret_access_key = secret_access_key self.associate_tag = associate_tag self.version = "2009-10-01" self.url = None def setProxy(self, host, port=8080): """ãããã·ãã»ãã""" self.proxy_host = host self.proxy_port = port def setVersion(self, version): """ãã¼ã¸ã§ã³ãã»ãã""" self.version = version def itemLookup(self, item_id, **options): """ã¢ã¤ãã ã®è©³ç´°æ å ±ãåå¾""" params = options params["Operation"] = "ItemLookup" params["ItemId"] = item_id return self.sendRequest(params) def itemSearch(self, search_index, **options): """ã¢ã¤ãã ãæ¤ç´¢""" params = options params["Operation"] = "ItemSearch" params["SearchIndex"] = search_index return self.sendRequest(params) def buildURL(self, params): """RESTãªã¯ã¨ã¹ãã®URLã¢ãã¬ã¹ãæ§ç¯""" params["Service"] = "AWSECommerceService" params["AWSAccessKeyId"] = self.access_key if self.associate_tag is not None: params["AssociateTag"] = self.associate_tag params["Timestamp"] = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()) sorted_params = sorted(params.items()) # paramsã®ããã·ã¥ãå±é request = [] for p in sorted_params: pair = "%s=%s" % (p[0], urllib2.quote(p[1].encode("utf-8"))) request.append(pair) # 2009/8/15ããèªè¨¼ãå°å ¥ããã¦ãã # Secret Access Keyã使ã£ã¦HMAC-SHA256ãè¨ç® msg = "GET\nwebservices.amazon.co.jp\n/onca/xml\n%s" % ("&".join(request)) hmac_digest = hmac.new(self.secret_access_key, msg, hashlib.sha256).digest() base64_encoded = base64.b64encode(hmac_digest) signature = urllib2.quote(base64_encoded) # Signatureããªã¯ã¨ã¹ãã«è¿½å ãã¦URLãä½æ request.append("Signature=%s" % signature) url = self.amazonurl + "?" + "&".join(request) return url def sendRequest(self, params): """Amazonã«ãªã¯ã¨ã¹ããéä»ããåå¾ããXMLãè¿ã""" self.url = self.buildURL(params) if self.proxy_host: proxy_handler = urllib2.ProxyHandler({"http":"http://%s:%s/" % (self.proxy_host, self.proxy_port)}) opener = urllib2.build_opener(proxy_handler) else: opener = urllib2.build_opener() return opener.open(self.url).read()
ãã¼ã¯ã¼ãã§æ¬ãæ¤ç´¢ãããï¼
Amazon APIã使ãã«ã¯Access Key IDã¨Secret Access Keyãå¿ è¦ã«ãªãã¾ããç»é²ã¯ここããè¡ãã¾ããSecret Access Keyã¯ã·ã°ããã£ã®ç§å¯éµãªã®ã§å ¬éãã¦ã¯ãã¡ã§ãããããªããã§ä¸ã®ããã°ã©ã ã§ã¯èªåã§åå¾ããIDã使ã£ã¦ãã ããããã¨ä¸ã®ãµã³ãã«ããã°ã©ã ã¯ãAmazonããè¿ã£ã¦ããXMLã®ãã¼ã¹ã«BeautifulSoupã使ã£ã¦ãã¾ãããã¡ããã¡ã便å©ãªã®ã§ãã²å°å ¥ããã»ããããã§ãã
ã§ã¯ãAmazonã¯ã©ã¹ã使ã£ã¦ã人工ç¥è½ãã¨ãããã¼ã¯ã¼ãã§æ¬ãæ¤ç´¢ãã¦ã¿ã¾ãã
#coding:utf-8 from amazon import Amazon # ãã¼ã¯ã¼ãã§æ¬ãæ¤ç´¢ãããï¼ amazon = Amazon("[èªåã®Access Key使ã£ã¦ãï¼]", "[èªåã®Secret Access Key使ã£ã¦ãï¼]") xml = amazon.itemSearch("Books", Keywords="人工ç¥è½", ItemPage="1") # æ¬ print amazon.url # ãªã¯ã¨ã¹ãURL #print xml # Amazonã®ã¬ã¹ãã³ã¹ # XMLããæ å ±ãåãåºã from BeautifulSoup import BeautifulStoneSoup soup = BeautifulStoneSoup(xml) items = soup.find("items") print "%s件è¦ã¤ããã¾ãã" % soup.find("totalresults").contents[0] total_pages = soup.find("totalpages").contents[0] cur_page = soup.find("itempage").contents[0] print "ãã¼ã¸æ°: %s/%s" % (cur_page, total_pages) for item in soup.findAll("item"): print item.asin.contents[0], item.author.contents[0], item.title.contents[0]
å®è¡çµæã¯ãä¸ã®ããã«ãªãã¾ãã
http://webservices.amazon.co.jp/onca/xml?AWSAccessKeyId=0CDQFTTPS2NZ96X08C02&ItemPage=1&Keywords=%E4%BA%BA%E5%B7%A5%E7%9F%A5%E8%83%BD&Operation=ItemSearch&SearchIndex=Books&Service=AWSECommerceService&Timestamp=2010-01-16T12%3A55%3A51Z&Signature=yezZ2JcRjzwys3G1%2B7eWXFc/U8aHYzYh7EQR729kS18%3D 741件è¦ã¤ããã¾ãã ãã¼ã¸æ°: 1/75 4320121163 èå± çäº äººå·¥ç¥è½æ¦è« 第2ç âã³ã³ãã¥ã¼ã¿ç¥è½ããWebç¥è½ã¾ã§ 4320122151 S.J.Russell ã¨ã¼ã¸ã§ã³ãã¢ããã¼ã人工ç¥è½ 第2ç 4270000600 ã¸ã§ãã»ãã¼ãã³ã¹ èããè³ èããã³ã³ãã¥ã¼ã¿ã¼ 4883170802 æ£®å· å¹¸äºº ãããç®±ã®è³(AI)â使ãã人工ç¥è½ã®ã話 4627803028 è å ç 次 人工ç¥è½ (æ å ±å·¥å¦å ¥éã·ãªã¼ãº) 4757211015 ã¹ãã£ã¼ã´ã»ã°ã©ã³ã ã¢ã³ããã¤ãã®è³ 人工ç¥è½ãããã"ã«ã¼ã·ã¼"ãèªçãããã¾ã§ã®ç°¡åãª20ã®ã¹ããã 4764901455 太å è²å¤« 人工ç¥è½ã®åºç¤ç¥è (ã³ã³ãã¥ã¼ã¿ãµã¤ã¨ã³ã¹å¤§å¦è¬åº§) 4339023876 æ°è°· èæ¾ Javaã«ããç¥è½ããã°ã©ãã³ã°å ¥é 4274201066 æ¾æ¬ ä¸æ 人工ç¥è½ (IT Text) 4274066649 å°é« ç¥å® ã¯ããã¦ã®AIããã°ã©ãã³ã°âCè¨èªã§ä½ã人工ç¥è½ã¨äººå·¥ç¡è½
1åã®ãªã¯ã¨ã¹ãã§ã¯10件ããåå¾ã§ããªãã¿ããã§ãã2ãã¼ã¸ç®ä»¥éã®æ¤ç´¢çµæãåå¾ããã«ã¯ItemPageãã©ã¡ã¼ã¿ãå¤æ´ãã¦å度ãªã¯ã¨ã¹ããåºãå¿ è¦ãããã¾ãã
ISBNã§æ¬ã®æ å ±ãåå¾ãããï¼
ASINãISBNã使ã£ã¦ããã«æ¬ã®è©³ç´°æ å ±ãåå¾ã§ãã¾ããä¸ã®ä¾ã§ã¯ãæ¬ã«ã¤ãã¦ãã13æ¡ã®ISBNã使ã£ã¦æ¤ç´¢ãã¦ã¾ããIdTypeãASINã«ããã°ãASINã§ãæ¤ç´¢ã§ãã¾ãããã£ãã®4320121163ã¨ããASINã§ãã
#coding:utf-8 from amazon import Amazon # ISBNã§æ¬ã®æ å ±ãåå¾ãããï¼ amazon = Amazon("[èªåã®Access Key使ã£ã¦ãï¼]", "[èªåã®Secret Access Key使ã£ã¦ãï¼]") xml = amazon.itemLookup("9784431100133", SearchIndex="Books", IdType="ISBN", ResponseGroup="Large,Similarities", ReviewPage="1") # æ¬ print amazon.url # ãªã¯ã¨ã¹ãURL #print xml # Amazonã®ã¬ã¹ãã³ã¹ # XMLããæ å ±ãåãåºã from BeautifulSoup import BeautifulStoneSoup soup = BeautifulStoneSoup(xml) item = soup.find("item") # ååæ å ± print item.asin # Amazonååã³ã¼ã print item.detailpageurl # ååãã¼ã¸ã®URL print item.smallimage.url # å°ç»åã¸ã®URL print item.mediumimage.url # ä¸ç»åã¸ã®URL print item.largeimage.url # 大ç»åã¸ã®URL print item.largeimage.height # ã¤ã¡ã¼ã¸ã®é«ã print item.largeimage.width # ã¤ã¡ã¼ã¸ã®å¹ # ååå±æ§ print item.itemattributes.findAll("author") # èè ãªã¹ã print item.itemattributes.findAll("creator") # ä½è print item.itemattributes.ean # EAN print item.itemattributes.isbn # ISBN print item.itemattributes.listprice.amount # ä¾¡æ ¼ print item.itemattributes.publicationdate # åºçæ¥ print item.itemattributes.publisher # åºçå± print item.itemattributes.title # ã¿ã¤ãã« print item.itemattributes.numberofpages # ãã¼ã¸æ° # ã¬ãã¥ã¼é¢ä¿ print item.customerreviews.averagerating # å¹³åã¬ã¤ãã£ã³ã° print item.customerreviews.totalreviews # ã¬ãã¥ã¼æ° print item.customerreviews.totalreviewpages # ã¬ãã¥ã¼ãã¼ã¸æ° # ã¬ãã¥ã¼æ¬æ for review in item.customerreviews.findAll("review"): print review.summary, review.rating # è¦ç´ã¨ã¬ã¤ãã£ã³ã° # print review.content # ã¬ãã¥ã¼æ¬æ # é¢é£åå for similar in item.similarproducts.findAll("similarproduct"): print similar.asin, similar.title
å®è¡çµæã¯ãä¸ã®ããã«ãªãã¾ããããããªæ å ±ãåå¾ã§ãã¾ãã
http://webservices.amazon.co.jp/onca/xml?AWSAccessKeyId=0CDQFTTPS2NZ96X08C02&IdType=ISBN&ItemId=9784431100133&Operation=ItemLookup&ResponseGroup=Large%2CSimilarities&ReviewPage=1&SearchIndex=Books&Service=AWSECommerceService&Timestamp=2010-01-16T12%3A59%3A48Z&Signature=IERGvNZpi2A0dpOaV/nw9ZuzeMzPF0tirC%2BcW3k1iD4%3D <asin>443110013X</asin> <detailpageurl>http://www.amazon.co.jp/%E3%83%91%E3%82%BF%E3%83%BC%E3%83%B3%E8%AA%8D%E8%AD%98%E3%81%A8%E6%A9%9F%E6%A2%B0%E5%AD%A6%E7%BF%92-%E3%83%99%E3%82%A4%E3%82%BA%E7%90%86%E8%AB%96%E3%81%AB%E3%82%88%E3%82%8B%E7%B5%B1%E8%A8%88%E7%9A%84%E4%BA%88%E6%B8%AC-C-M-%E3%83%93%E3%82%B7%E3%83%A7%E3%83%83%E3%83%97/dp/443110013X%3FSubscriptionId%3D0CDQFTTPS2NZ96X08C02%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D443110013X</detailpageurl> <url>http://ecx.images-amazon.com/images/I/41z0AD06ZaL._SL75_.jpg</url> <url>http://ecx.images-amazon.com/images/I/41z0AD06ZaL._SL160_.jpg</url> <url>http://ecx.images-amazon.com/images/I/41z0AD06ZaL.jpg</url> <height units="pixels">459</height> <width units="pixels">300</width> [<author>C. M. ãã·ã§ãã</author>] [<creator role="翻訳">å ç° æµ©</creator>, <creator role="翻訳">æ ç° å¤å夫</creator>, <creator role="翻訳">æ¨å£ ç¥ä¹</creator>, <creator role="翻訳">æ¾æ¬ è£æ²»</creator>, <creator role="翻訳">æç° æ</creator>] <ean>9784431100133</ean> <isbn>443110013X</isbn> <amount>6825</amount> <publicationdate>2007-12-10</publicationdate> <publisher>ã·ã¥ããªã³ã¬ã¼ã»ã¸ã£ãã³æ ªå¼ä¼ç¤¾</publisher> <title>ãã¿ã¼ã³èªèã¨æ©æ¢°å¦ç¿ ä¸ - ãã¤ãºçè«ã«ããçµ±è¨çäºæ¸¬</title> <numberofpages>349</numberofpages> <averagerating>3.5</averagerating> <totalreviews>2</totalreviews> <totalreviewpages>1</totalreviewpages> <summary>ãªãªã¸ãã«ã«å£ã...</summary> <rating>2</rating> <summary>æ©æ¢°å¦ç¿ã®æ°ã»å®çªæç§æ¸ï¼å¾ æã®é¦è¨³ï¼</summary> <rating>5</rating> <asin>4431100318</asin> <title>ãã¿ã¼ã³èªèã¨æ©æ¢°å¦ç¿ ä¸ - ãã¤ãºçè«ã«ããçµ±è¨çäºæ¸¬</title> <asin>4431727388</asin> <title>çµ±è¨ã®ããã®è¡åä»£æ° ä¸ (2)</title> <asin>443172737X</asin> <title>çµ±è¨ã®ããã®è¡åä»£æ° ä¸ (1)</title> <asin>4627847114</asin> <title>ããªã¼ã½ããã§ã¤ããé³å£°èªèã·ã¹ãã - ãã¿ã¼ã³èªèã»æ©æ¢°å¦ç¿ã®åæ©ãã対話ã·ã¹ãã ã¾ã§</title> <asin>4000069713</asin> <title>ã«ã¼ãã«å¤å¤é解æâéç·å½¢ãã¼ã¿è§£æã®æ°ããå±é (ã·ãªã¼ãºç¢ºçã¨æ å ±ã®ç§å¦)</title>
titleã¨ãã®ã¿ã°ããããªãå ´åã¯ã
print item.itemattributes.title.contents[0] # ã¿ã¤ãã«
ã¨contents[0]ãã¤ããã°OKã§ããç»åã¯URLããããã®ã§urllib2ã§ã¢ã¯ã»ã¹ãã¦jpegãåå¾ã§ãã¾ãããã®APIã§ã¯ãæ¬ä»¥å¤ã«ãããããªæ å ±ãåå¾ã§ãã¾ãã詳ããã¯開発者ガイドï¼è±èªï¼ãåç §ãã¦ãã ããã