Pythonã§Amazonã®ååãã¼ã¸ããã¼ã¹ãã¦asinããæ¸ç±åãåå¾ãã
ã¨ããçç±ã§Pythonã§ASINâæ¸ç±åå¤æããããã¨æãã¾ãã
åã¯Amazon APIã¨ãããã®ããã£ã¦æ¥½ã«åå¾ã§ããè¨æ¶ããããã§ãããä»ã¯ã©ãããèªè¨¼ã®æç¶ããå¿ è¦ã«ãªã£ãããããååãå¤ãã£ã¦ãã¾ããã
èªè¨¼ã«ã·ã¼ã¯ã¬ãããã¼ã¯ã³ããsha256ã®hmacãããªãããããã©ããããããé常ã®ååãã¼ã¸ã«æ£è¦è¡¨ç¾ãããæ¹ã楽ãªããããªããã¨æã£ãã®ã§ãã£ã¦ã¿ã¾ããã
metaã¿ã°ã®titleã³ã³ãã³ãã«ããã£ã½ãæ å ±ãå«ã¾ãã¦ããã®ã§ãããåå¾ãã¾ããããã®å 容ã¯ä»¥ä¸ã®ãããªãã©ã¼ãããã«ãªã£ã¦ãããã§ãã
Amazon.co.jp: ï¼ã¿ã¤ãã«ï¼: ï¼ã«ã³ãåºåãã®èè ãªã¹ãï¼: ï¼åé¡ï¼
ï¼ã¿ã¤ãã«ï¼ãï¼åé¡ï¼ã¯ããããunicodeã®æ°å¤åç §å½¢å¼(XXXX;)ã«ãªã£ã¦ããã®ã§ãããå®éã®unicodeæååã«å¤æããå¦çããã¦ãã¾ãã
ãã£ã¦ã¿ã¦ããæ°ãä»ãã¾ããããamazonã®ååãã¼ã¸ã¯ï¼ãã¼ã¸ãããã®å®¹éã60KBããããã£ã¦æå¤ã«ã§ããã®ã§ãé度ãã»ããå ´åã¯APIãå©ããæ¹ãè¯ãããããã¾ããã
ãã¼ã¸ã®HTMLã®æ§é ãå¤æ´ãããå ´ååããªããªãå¯è½æ§ãããã®ã§æ³¨æãã¦ãã ããã
#! /usr/bin/python.26 # coding: utf-8 import urllib import re def searchBookFromAmazon(asin): try: conn = urllib.urlopen('http://amazon.jp/dp/' + str(asin)) regexp = re.compile(r'<meta name="title" content="(.*?)".*') line = regexp.findall(conn.read())[0] def ref2uni(s): s = s.replace(';', '').replace('&#x', '\u') s = eval('u"' + s + '"') return s line = line.split(': ')[1] # èè åãªã¹ãã«': 'ãå«ã¾ãã¦ããã¨æå³ã©ããã«åããªã # å«ã¾ãã¦ããã±ã¼ã¹ãããã®ãã©ããã¯ç¥ããªã sepindex = line.rindex(': ') lst = line[:sepindex], line[sepindex + 2:] sepindex = lst[0].rindex(': ') lst = lst[0][:sepindex], lst[0][sepindex + 2:], lst[1] lst = map(ref2uni, lst) return dict(zip(['title', 'author'], lst[:-1])) except Exception, e: raise e def main(): import sys if len(sys.argv) < 2: print 'usage: %s asin' % __file__ return data = searchBookFromAmazon(sys.argv[1]) print 'title=%(title)s, author=%(author)s' % data if __name__ == '__main__': main()
å®è¡çµæ
以ä¸ã®ãã¹ããã¼ã¿ã§ãã¹ããã¾ããããã¾ãè¡ããªãä¾ãããããã
$ cat Dropbox/dat.txt 4873114616 4893116487 4873113725 1430227907 4839928258 4413003012 4152091312 4022645237 459406194X 4048688782 $ for n in $(cat Dropbox/dat.txt); do python amabook.py $n; sleep 3; done title=iPhoneã¢ããªã±ã¼ã·ã§ã³éçºã¬ã¤ããâHTML+CSS+JavaScript ã«ããéçºææ³, author=Jonathan Stark, å¢äº ä¿ä¹, ç§é è¡ title=ãããããã¼ãããã¯ãããJavaScript (ããããããã¬ã¼ãã³ã°ããã¹ã), author=å¯å£«éãªãã£ã¹æ©å¨æ ªå¼ä¼ç¤¾ title=Prototype & script.aculo.us âJavaScriptã©ã¤ãã©ãªã«ããAjaxã¢ããªã±ã¼ã·ã§ã³éçº, author=Christophe Porteneuve, æ å±± æ·³(ç£è¨³), åç° é¼äº title=Pro HTML5 Programming: Powerful APIs for Richer Internet Application Development, author=Peter Lubbers, Brian Albers, Ric Smith, Frank Salim title=å®è·µ!Ajaxãã¬ã¼ã ã¯ã¼ã¯ Prototype.js/Script.aculo.us, author=æ²æ æ£ç´ title=試é¨ã«ã§ãè±åèªâè³ããè¦ãã, author=森 ä¸é title=ããããã®ãæ£ç¾©ãã®è©±ããããââãã¾ãçã延ã³ãããã®å²å¦, author=ãã¤ã±ã«ã»ãµã³ãã«, Michael J. Sandel, 鬼澤 å¿ title=æªäºº(ä¸) (ææ¥æ庫), author=åç° ä¿®ä¸ title=ã¢ã ãã£ã³ãã¤ã¨ãã ãã¬ãã¢ã (DVDä»ã), author=ãã§ã³ã»ãã¨ã³ title=ã´ã¼ã«ãã³ã¿ã¤ã ã1ãæ¥ã«ãã¦ãã©ãã¯ã¢ã¦ã (é»ææ庫), author=竹宮 ããã
ã¨ãã¹ãã¼ãPythonããã°ã©ãã³ã°
- ä½è : Tarek Ziade,稲ç°ç´å,æ¸å·ããã,æ¸ æ°´å·è²´ä¹,森æ¬å²ä¹
- åºç社/ã¡ã¼ã«ã¼: KADOKAWA/ã¢ã¹ãã¼ã»ã¡ãã£ã¢ã¯ã¼ã¯ã¹
- çºå£²æ¥: 2010/05/28
- ã¡ãã£ã¢: 大åæ¬
- è³¼å ¥: 33人 ã¯ãªãã¯: 791å
- ãã®ååãå«ãããã° (90件) ãè¦ã