{"id":166,"date":"2009-08-29T01:35:33","date_gmt":"2009-08-28T16:35:33","guid":{"rendered":"http:\/\/www.typea.info\/blog\/index.php\/2009\/08\/29\/python_elementtree_amazon_api\/"},"modified":"2009-08-29T01:35:33","modified_gmt":"2009-08-28T16:35:33","slug":"python_elementtree_amazon_api","status":"publish","type":"post","link":"https:\/\/www.typea.info\/blog\/index.php\/2009\/08\/29\/python_elementtree_amazon_api\/","title":{"rendered":"Python ElementTree \u3067Amazon API\u3092\u5229\u7528\u3059\u308b"},"content":{"rendered":"<p><a href=\"\/blg\/glob\/2009\/08\/amazon_product_advertising_api_1.html\">\u3053\u306e\u9593\u3001SAX\u30d1\u30fc\u30b5\u30fc\u3067Amazon Product(\u4ee5\u4e0b\u7565)\u3092\u89e3\u6790\u3057\u305f<\/a>\u306e\u3067\u3001\u66f4\u306a\u308bXML\u67d4\u8edf\u5229\u7528\u3092\u76ee\u6307\u3057\u3066XPath\u3092\u4f7f\u3063\u3066\u307f\u308b\u3002<\/p>\n<p>Amazon API\u3078\u306eREST\u30ea\u30af\u30a8\u30b9\u30c8\u4f5c\u6210\u306f<a href=\"\/blg\/glob\/2009\/07\/amazon_product_advertising_api.html\">\u3053\u3061\u3089<\/a>\u3002\u307e\u305f\u3001\u6700\u65b0\u306e\u30bd\u30fc\u30b9\u306f<a href=\"http:\/\/code.google.com\/p\/typea-info-apps\/source\/browse\/#svn\/trunk\">\u3053\u306e\u3042\u305f\u308a<\/a>\u3092<\/p>\n<p><strong>ElementTree \u53c2\u8003URL<\/strong><\/p>\n<ul>\n<li><a title=\"http:\/\/pythonjp.sourceforge.jp\/dev\/library\/xml.etree.elementtree.html\" href=\"http:\/\/pythonjp.sourceforge.jp\/dev\/library\/xml.etree.elementtree.html\">http:\/\/pythonjp.sourceforge.jp\/dev\/library\/xml.etree.elementtree.html<\/a>\n<li><a title=\"http:\/\/www.python.org\/doc\/current\/library\/xml.etree.elementtree.html\" href=\"http:\/\/www.python.org\/doc\/current\/library\/xml.etree.elementtree.html\">http:\/\/www.python.org\/doc\/current\/library\/xml.etree.elementtree.html<\/a>\n<li><a title=\"http:\/\/www.python.jp\/doc\/release\/lib\/module-xml.etree.ElementTree.html\" href=\"http:\/\/www.python.jp\/doc\/release\/lib\/module-xml.etree.ElementTree.html\">http:\/\/www.python.jp\/doc\/release\/lib\/module-xml.etree.ElementTree.html<\/a> <\/li>\n<\/ul>\n<p><strong>XPath \u306e\u69cb\u6587<\/strong><\/p>\n<ul>\n<li>\n<pre class=\"pretyprint\"><a title=\"\/tips\/wiki.cgi?page=XPath\" href=\"\/tips\/wiki.cgi?page=XPath\">\/tips\/wiki.cgi?page=XPath<\/a><\/pre>\n<li><a title=\"http:\/\/www.techscore.com\/tech\/XML\/XPath\/xpath03.html\" href=\"http:\/\/www.techscore.com\/tech\/XML\/XPath\/xpath03.html\">http:\/\/www.techscore.com\/tech\/XML\/XPath\/xpath03.html<\/a> <\/li>\n<\/ul>\n<p><strong>\u6ce8\u610f\u70b9<\/strong> <\/p>\n<ul>\n<li><a href=\"http:\/\/typea-mixi01.appspot.com\/yh_s?q=xmlns\">xmlns<\/a>\u5c5e\u6027\u304c\u3042\u308b\u3068\u3001\u30bf\u30b0\u3092\u6307\u5b9a\u3059\u308b\u3068\u304d\u306b\u3001&#8217;\/\/tag_name&#8217; \u306a\u3069\u3068\u66f8\u304f\u3053\u3068\u304c\u3067\u304d\u305a\u306b\u3001&#8217;\/\/{http:\/\/xmlns_attribute_value}tag_name&#8217;\u306e\u3088\u3046\u306b\u3044\u3061\u3044\u3061\u8a18\u8ff0\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002\n<li>\u8981\u3059\u308b\u306b\u3001elm.find(&#8216;.\/ItemAttributes\/Title&#8217;) \u3068\u66f8\u304f\u3053\u3068\u306f\u3067\u304d\u305a\u3001elm.find(&#8216;.\/{http:\/\/webservices.amazon.com\/AWSECommerceService\/2005-10-05}ItemAttributes\/{http:\/\/webservices.amazon.com\/AWSECommerceService\/2005-10-05}Title \u306e\u3088\u3046\u306b\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b&#8217;) <\/li>\n<\/ul>\n<pre class=\"prettyprint linenums\">#!Python2.6\r\n# -*- coding: utf-8 -*-\r\n\r\nimport amazon_ecs  # \u81ea\u4f5c\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u30a4\u30f3\u30dd\u30fc\u30c8\r\nimport urllib2\r\nfrom xml.etree import ElementTree\r\n\r\n# xmlns \u3092\u4ed8\u52a0\u3057\u305f\u30bf\u30b0\u540d\u3092\u8fd4\u3059\r\ndef qn(tag):\r\n    return ElementTree.QName(ns, tag).text\r\n\r\nns = r'http:\/\/webservices.amazon.com\/AWSECommerceService\/2005-10-05'\r\n\r\n# xmlns\u304c\u6307\u5b9a\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u3001\u30bf\u30b0\u3092{xmlns\u306e\u5024}\u30bf\u30b0\u540d \u3068\u3044\u3061\u3044\u3061\u66f8\u304f\u5fc5\u8981\u304c\u3042\u308b\u305f\u3081\u3001\u305d\u306e\u3088\u3046\u306b\u5c55\u958b\u3057\u305f\u3082\u306e\u3092\u4fdd\u6301\u3057\u3066\u304a\u304f\r\n# .\/{http:\/\/webservices.amazon.com\/AWSECommerceService\/2005-10-05}ItemAttributes\/{http:\/\/webservices.amazon.com\/AWSECommerceService\/2005-10-05}Title\r\nq_items  = '.\/\/{0}'.format(qn('Item'))\r\nq_title  = '.\/{0}\/{1}'.format(qn('ItemAttributes'), qn('Title'))\r\nq_author = '.\/{0}\/{1}'.format(qn('ItemAttributes'), qn('Author'))\r\nq_asin   = '.\/{0}'.format(qn('ASIN'))\r\nq_url    = '.\/{0}'.format(qn('DetailPageURL'))\r\nq_img    = '.\/{0}\/{1}'.format(qn('SmallImage'), qn('URL'))\r\n\r\n# Amazon Product Advertise API \u30ea\u30af\u30a8\u30b9\u30c8 URL\u3092\u751f\u6210\r\noperation = amazon_ecs.ItemSearch()\r\noperation.keywords('\u624b\u585a\u3000\u6cbb\u866b')\r\noperation.search_index('Books')\r\noperation.response_group('Large')\r\nrequest = operation.request()\r\nprint 'REQUEST : {0}'.format(request)\r\n\r\n# ElementTree\u3092\u751f\u6210\r\nroot = ElementTree.parse(urllib2.urlopen(request)).getroot()\r\n# XPath\u3092\u4f7f\u7528\u3057\u3066ElementTree\u3092\u89e3\u6790\r\nitems =  root.findall(q_items)\r\nfor item in items:\r\n    print '-' * 100\r\n    print 'TITLE : {0}'.format(item.find(q_title).text)\r\n    print 'AUTHOR : {0}'.format(item.find(q_author).text)\r\n    print 'ASIN : {0}'.format(item.find(q_asin).text)\r\n    print 'URL : {0}'.format(item.find(q_url).text)\r\n    print 'IMG : {0}'.format(item.find(q_img).text)\r\n<\/pre>\n<p><strong>PyDev\u3067\u8d70\u3089\u305b\u305f\u7d50\u679c<\/strong> <\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/PythonElementTreeAmazonAPI_F75\/py_xpath01.jpg\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" border=\"0\" alt=\"py_xpath01\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/PythonElementTreeAmazonAPI_F75\/py_xpath01_thumb.jpg\" width=\"391\" height=\"204\"><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u306e\u9593\u3001SAX\u30d1\u30fc\u30b5\u30fc\u3067Amazon Product(\u4ee5\u4e0b\u7565)\u3092\u89e3\u6790\u3057\u305f\u306e\u3067\u3001\u66f4\u306a\u308bXML\u67d4\u8edf\u5229\u7528\u3092\u76ee\u6307\u3057\u3066XPath\u3092\u4f7f\u3063\u3066\u307f\u308b\u3002 Amazon API\u3078\u306eREST\u30ea\u30af\u30a8\u30b9\u30c8\u4f5c\u6210\u306f\u3053\u3061\u3089\u3002\u307e\u305f\u3001\u6700\u65b0\u306e\u30bd\u30fc\u30b9\u306f\u3053\u306e\u3042\u305f\u308a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"sns_share_botton_hide":"","vkExUnit_sns_title":"","_vk_print_noindex":"","sitemap_hide":"","_veu_custom_css":"","veu_display_promotion_alert":"","vkexunit_cta_each_option":"","footnotes":""},"categories":[25,20],"tags":[],"class_list":["post-166","post","type-post","status-publish","format-standard","hentry","category-amazon-web-services","category-python"],"veu_head_title_object":{"title":"","add_site_title":""},"_links":{"self":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/166","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=166"}],"version-history":[{"count":0,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/166\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}