読者です 読者をやめる 読者になる 読者になる

oneshotlife-pythonのブログ

Pythonを好んで使っているプログラマです。Pythonこそが最強のプログラミング言語だと思っています。Pythonは使いやすいです。Pythonは書きやすいです。Pythonは読みやすいです。Pythonはパワフルです。Pythonは一貫性があります。Pythonが好きです。

Pythonで楽天ウェブサービスを使って書籍情報を取得する #Python

楽天ウェブサービスを使って情報を取得してみた。

今回使ったのは、書籍の検索のapi。著者情報を渡して、その著者の書籍を取得するというもの。要求はhttpで投げて、レスポンスはjsonで受け取りました。jsonの情報って複雑・・・。

サンプルコード

#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
import urllib
import urllib2
import json

def Rakuten_BooksAuthorSearch(author,hits,sort):
    developerId = 'DEV_ID'
    affiliateId = 'AFF_ID'
    uri = 'https://app.rakuten.co.jp/services/api/BooksBook/Search/20130522?'+\
    'applicationId='   +developerId+\
    '&affiliateId='    +affiliateId+\
    '&author='          +author+\
    '&hits='            +hits+\
    '&sort='            +sort
    
    res = json.load(urllib2.urlopen(uri))
    print json.dumps(res, sort_keys = True, indent = 4)
    
    items = res["Items"]
    for item in items:
        print item["Item"]["title"]       
    print """<!-- Rakuten Web Services Attribution Snippet FROM HERE -->
<a href="http://webservice.rakuten.co.jp/" target="_blank">Supported by 楽天ウェブサービス</a>
<!-- Rakuten Web Services Attribution Snippet TO HERE -->"""

Rakuten_BooksAuthorSearch(urllib.quote("芥川龍之介"),"5","sales")

ポイント

jsonのデータをロードするのは意外と簡単!

res = json.load(urllib2.urlopen(uri))

たったこれだけ。

あと、jsonのデータをヴィジュアル化して出力するdumpのメソッドも便利。

print json.dumps(res, sort_keys = True, indent = 4)

実行結果

{
    "GenreInformation": [], 
    "Items": [
        {
            "Item": {
                "affiliateUrl": "http://hb.afl.rakuten.co.jp/hgc/g00q0723.hqkjfc3b.g00q0723.hqkjg1ee/?pc=http%3A%2F%2Fbooks.rakuten.co.jp%2Frb%2F4880397%2F&m=http%3A%2F%2Fm.rakuten.co.jp%2Frms%2Fmsv%2FItem%3Fn%3D4880397%26surl%3Dbook", 
                "author": "\u82a5\u5ddd\u9f8d\u4e4b\u4ecb/\u30d0\u30e9\u30a8\u30c6\u30a3\u30fb\u30a2\u30fc\u30c8\u30ef\u30fc\u30af\u30b9", 
                "authorKana": "\u30a2\u30af\u30bf\u30ac\u30ef,\u30ea\u30e5\u30a6\u30ce\u30b9\u30b1/\u30d0\u30e9\u30a8\u30c6\u30a3 \u30a2\u30fc\u30c8 \u30ef\u30fc\u30af\u30b9", 
                "availability": "1", 
                "booksGenreId": "001001012/001019001/001019007", 
                "chirayomiUrl": "", 
                "contents": "", 
                "discountPrice": 0, 
                "discountRate": 0, 
                "isbn": "9784872578348", 
                "itemCaption": "\u5929\u707d\u3084\u98e2\u9913\u306b\u82e6\u3057\u3080\u5e73\u5b89\u6642\u4ee3\u306e\u4eac\u90fd\u3002\u90fd\u306e\u30b7\u30f3\u30dc\u30eb\u7f85\u751f\u9580\u306b\u3082\u76d7\u4eba\u304c\u4f4f\u307f\u3001\u6b7b\u4eba\u304c\u6368\u3066\u3089\u308c\u8352\u5ec3\u3057\u3066\u3044\u305f\u3002\u5c4b\u6577\u3092\u30ea\u30b9\u30c8\u30e9\u3055\u308c\u8def\u982d\u306b\u8ff7\u3046\u4f7f\u7528\u4eba\u304c\u3001\u6b7b\u4eba\u306e\u9aea\u306e\u6bdb\u3092\u76d7\u3080\u8001\u5a46\u3068\u51fa\u4f1a\u3044\u3001\u751f\u304d\u308b\u305f\u3081\u306e\u624b\u6bb5\u3092\u6a21\u7d22\u3059\u308b\u8868\u984c\u4f5c\u300c\u7f85\u751f\u9580\u300d\u3002\u305d\u306e\u4ed6\u3001\u5927\u6b63\u6587\u58c7\u306e\u5bf5\u5150\u304c\u4eca\u6614\u7269\u8a9e\u3092\u984c\u6750\u306b\u63cf\u3044\u305f\u300c\u738b\u671d\u3082\u306e\u300d\u3068\u547c\u3070\u308c\u308b\u4f5c\u54c1\u5168\uff13\u7de8\u3092\u6f2b\u753b\u5316\u3002", 
                "itemPrice": 596, 
                "itemUrl": "http://books.rakuten.co.jp/rb/4880397/", 
                "largeImageUrl": "http://thumbnail.image.rakuten.co.jp/@0_mall/book/cabinet/8725/87257834.jpg?_ex=200x200", 
                "limitedFlag": 0, 
                "listPrice": 0, 
                "mediumImageUrl": "http://thumbnail.image.rakuten.co.jp/@0_mall/book/cabinet/8725/87257834.jpg?_ex=120x120", 
                "postageFlag": 0, 
                "publisherName": "\u30a4\u30fc\u30b9\u30c8\u30fb\u30d7\u30ec\u30b9", 
                "reviewAverage": "4.33", 
                "reviewCount": 12, 
                "salesDate": "2007\u5e7410\u6708", 
                "seriesName": "\u307e\u3093\u304c\u3067\u8aad\u7834", 
                "seriesNameKana": "\u30de\u30f3\u30ac \u30c7 \u30c9\u30af\u30cf", 
                "size": "\u6587\u5eab", 
                "smallImageUrl": "http://thumbnail.image.rakuten.co.jp/@0_mall/book/cabinet/8725/87257834.jpg?_ex=64x64", 
                "subTitle": "", 
                "subTitleKana": "", 
                "title": "\u7f85\u751f\u9580", 
                "titleKana": "\u30e9\u30b7\u30e7\u30a6\u30e2\u30f3"
            }
        }, 
        {
            "Item": {
                "affiliateUrl": "http://hb.afl.rakuten.co.jp/hgc/g00q0723.hqkjfc3b.g00q0723.hqkjg1ee/?pc=http%3A%2F%2Fbooks.rakuten.co.jp%2Frb%2F234333%2F&m=http%3A%2F%2Fm.rakuten.co.jp%2Frms%2Fmsv%2FItem%3Fn%3D234333%26surl%3Dbook", 
                "author": "\u82a5\u5ddd\u9f8d\u4e4b\u4ecb", 
                "authorKana": "\u30a2\u30af\u30bf\u30ac\u30ef,\u30ea\u30e5\u30a6\u30ce\u30b9\u30b1", 
                "availability": "1", 
                "booksGenreId": "001008022007/001008022008", 
                "chirayomiUrl": "", 
                "contents": "\u8718\u86db\u306e\u7cf8\u30fb\u5730\u7344\u5909\u30fb\u5949\u6559\u4eba\u306e\u6b7b", 
                "discountPrice": 0, 
                "discountRate": 0, 
                "isbn": "9784480020826", 
                "itemCaption": "", 
                "itemPrice": 864, 
                "itemUrl": "http://books.rakuten.co.jp/rb/234333/", 
                "largeImageUrl": "http://thumbnail.image.rakuten.co.jp/@0_mall/book/cabinet/0826/9784480020826.jpg?_ex=200x200", 
                "limitedFlag": 0, 
                "listPrice": 0, 
                "mediumImageUrl": "http://thumbnail.image.rakuten.co.jp/@0_mall/book/cabinet/0826/9784480020826.jpg?_ex=120x120", 
                "postageFlag": 0, 
                "publisherName": "\u7b51\u6469\u66f8\u623f", 
                "reviewAverage": "5.0", 
                "reviewCount": 1, 
                "salesDate": "1986\u5e7410\u6708", 
                "seriesName": "\u3061\u304f\u307e\u6587\u5eab", 
                "seriesNameKana": "\u30c1\u30af\u30de \u30d6\u30f3\u30b3", 
                "size": "\u6587\u5eab", 
                "smallImageUrl": "http://thumbnail.image.rakuten.co.jp/@0_mall/book/cabinet/0826/9784480020826.jpg?_ex=64x64", 
                "subTitle": "", 
                "subTitleKana": "", 
                "title": "\u82a5\u5ddd\u9f8d\u4e4b\u4ecb\u5168\u96c6\uff082\uff09", 
                "titleKana": "\u30a2\u30af\u30bf\u30ac\u30ef \u30ea\u30e5\u30a6\u30ce\u30b9\u30b1 \u30bc\u30f3\u30b7\u30e5\u30a6"
            }
        }, 
        {
            "Item": {
                "affiliateUrl": "http://hb.afl.rakuten.co.jp/hgc/g00q0723.hqkjfc3b.g00q0723.hqkjg1ee/?pc=http%3A%2F%2Fbooks.rakuten.co.jp%2Frb%2F3636093%2F&m=http%3A%2F%2Fm.rakuten.co.jp%2Frms%2Fmsv%2FItem%3Fn%3D3636093%26surl%3Dbook", 
                "author": "\u82a5\u5ddd\u9f8d\u4e4b\u4ecb", 
                "authorKana": "\u30a2\u30af\u30bf\u30ac\u30ef,\u30ea\u30e5\u30a6\u30ce\u30b9\u30b1", 
                "availability": "1", 
                "booksGenreId": "001003001001/001003002006", 
                "chirayomiUrl": "", 
                "contents": "", 
                "discountPrice": 0, 
                "discountRate": 0, 
                "isbn": "9784591088630", 
                "itemCaption": "", 
                "itemPrice": 616, 
                "itemUrl": "http://books.rakuten.co.jp/rb/3636093/", 
                "largeImageUrl": "http://thumbnail.image.rakuten.co.jp/@0_mall/book/cabinet/8630/9784591088630.jpg?_ex=200x200", 
                "limitedFlag": 0, 
                "listPrice": 0, 
                "mediumImageUrl": "http://thumbnail.image.rakuten.co.jp/@0_mall/book/cabinet/8630/9784591088630.jpg?_ex=120x120", 
                "postageFlag": 0, 
                "publisherName": "\u30dd\u30d7\u30e9\u793e", 
                "reviewAverage": "5.0", 
                "reviewCount": 2, 
                "salesDate": "2005\u5e7409\u670801\u65e5", 
                "seriesName": "\u30dd\u30d7\u30e9\u30dd\u30b1\u30c3\u30c8\u6587\u5eab", 
                "seriesNameKana": "\u30dd\u30d7\u30e9 \u30dd\u30b1\u30c3\u30c8 \u30d6\u30f3\u30b3", 
                "size": "\u65b0\u66f8", 
                "smallImageUrl": "http://thumbnail.image.rakuten.co.jp/@0_mall/book/cabinet/8630/9784591088630.jpg?_ex=64x64", 
                "subTitle": "", 
                "subTitleKana": "", 
                "title": "\u8718\u86db\u306e\u7cf8", 
                "titleKana": "\u30af\u30e2 \u30ce \u30a4\u30c8"
            }
        }, 
        {
            "Item": {
                "affiliateUrl": "http://hb.afl.rakuten.co.jp/hgc/g00q0723.hqkjfc3b.g00q0723.hqkjg1ee/?pc=http%3A%2F%2Fbooks.rakuten.co.jp%2Frb%2F12807786%2F&m=http%3A%2F%2Fm.rakuten.co.jp%2Frms%2Fmsv%2FItem%3Fn%3D12807786%26surl%3Dbook", 
                "author": "\u82a5\u5ddd\u9f8d\u4e4b\u4ecb", 
                "authorKana": "\u30a2\u30af\u30bf\u30ac\u30ef,\u30ea\u30e5\u30a6\u30ce\u30b9\u30b1", 
                "availability": "1", 
                "booksGenreId": "001004008001", 
                "chirayomiUrl": "", 
                "contents": "", 
                "discountPrice": 0, 
                "discountRate": 0, 
                "isbn": "9784796405621", 
                "itemCaption": "\u9759\u304b\u306b\u96e8\u306e\u964d\u308b\u7f85\u751f\u9580\u306e\u4e0b\u3002\u96e8\u6b62\u307f\u3092\u5f85\u3064\u4e0b\u4eba\u304c\u4e00\u4eba\u3002\u884c\u304f\u5f53\u3066\u3082\u306a\u304f\u4eca\u591c\u7720\u308b\u5834\u6240\u3082\u306a\u3044\u3002\u6fe1\u308c\u305f\u9aea\u2026\u7e8f\u308f\u308a\u3064\u304f\u3088\u3046\u306a\u96e8\u30fc\u3002\u6c88\u3093\u3060\u601d\u8003\u306e\u679c\u3066\u306b\u3001\u4e0b\u4eba\u306f\u7f85\u751f\u9580\u306e\u697c\u3067\u4e00\u591c\u3092\u660e\u304b\u305d\u3046\u3068\u601d\u3044\u3064\u304f\u3002\u968e\u6bb5\u3092\u4e0a\u308a\u3001\u305d\u306e\u5148\u306b\u898b\u305f\u3082\u306e\u3068\u306f\u30fc\u2026\uff1f\u8868\u984c\u4f5c\u307b\u304b\u3001\u73e0\u7389\u306e\u77ed\u7de8\u3092\u53ce\u9332\u3002\u58f0\u512a\u30fb\u5bae\u91ce\u771f\u5b88\u304c\u7d21\u3050\u300c\u7f85\u751f\u9580\u300d\u540d\u5834\u9762\u629c\u7c8b\u306e\u6717\u8aad\uff23\uff24\u3092\u5c01\u5165\u3002", 
                "itemPrice": 1050, 
                "itemUrl": "http://books.rakuten.co.jp/rb/12807786/", 
                "largeImageUrl": "http://thumbnail.image.rakuten.co.jp/@0_mall/book/cabinet/5621/9784796405621.jpg?_ex=200x200", 
                "limitedFlag": 0, 
                "listPrice": 0, 
                "mediumImageUrl": "http://thumbnail.image.rakuten.co.jp/@0_mall/book/cabinet/5621/9784796405621.jpg?_ex=120x120", 
                "postageFlag": 0, 
                "publisherName": "\u6d77\u738b\u793e", 
                "reviewAverage": "4.0", 
                "reviewCount": 1, 
                "salesDate": "2014\u5e7406\u6708", 
                "seriesName": "\u6d77\u738b\u793e\u6587\u5eab", 
                "seriesNameKana": "\u30ab\u30a4\u30aa\u30a6\u30b7\u30e3 \u30d6\u30f3\u30b3", 
                "size": "\u6587\u5eab", 
                "smallImageUrl": "http://thumbnail.image.rakuten.co.jp/@0_mall/book/cabinet/5621/9784796405621.jpg?_ex=64x64", 
                "subTitle": "", 
                "subTitleKana": "", 
                "title": "\u7f85\u751f\u9580", 
                "titleKana": "\u30e9\u30b7\u30e7\u30a6\u30e2\u30f3"
            }
        }, 
        {
            "Item": {
                "affiliateUrl": "http://hb.afl.rakuten.co.jp/hgc/g00q0723.hqkjfc3b.g00q0723.hqkjg1ee/?pc=http%3A%2F%2Fbooks.rakuten.co.jp%2Frb%2F6531230%2F&m=http%3A%2F%2Fm.rakuten.co.jp%2Frms%2Fmsv%2FItem%3Fn%3D6531230%26surl%3Dbook", 
                "author": "\u82a5\u5ddd\u9f8d\u4e4b\u4ecb/\u5742\u672c\u4e45\u4f5c", 
                "authorKana": "\u30a2\u30af\u30bf\u30ac\u30ef,\u30ea\u30e5\u30a6\u30ce\u30b9\u30b1/\u30b5\u30ab\u30e2\u30c8,\u30ad\u30e5\u30a6\u30b5\u30af", 
                "availability": "1", 
                "booksGenreId": "001015", 
                "chirayomiUrl": "http://books.rakuten.co.jp/chirayomi/index.phtml?id=7G4i353J&path=8u/O4+3u3j&iid=9784834263022", 
                "contents": "", 
                "discountPrice": 0, 
                "discountRate": 0, 
                "isbn": "9784834263022", 
                "itemCaption": "\u85ae\u306e\u4e2d\u3067\u4f8d\u306e\u60e8\u6bba\u6b7b\u4f53\u3092\u767a\u898b\u3002\u5947\u5999\u306a\u4e8b\u306b\u76ee\u6483\u8005\u306e\u8a3c\u8a00\u306f\u3001\u4f55\u6545\u304b\u7686\u3001\u98df\u3044\u9055\u3063\u3066\u3044\u305f\uff01\u771f\u5b9f\u306f\u4f55\u51e6\u306b\uff1f\u4ed6\u306b\u4e00\u7de8\u3001\u540d\u4f5c\u300e\u7f85\u751f\u9580\u300f\u3092\u53ce\u9332\u3002", 
                "itemPrice": 566, 
                "itemUrl": "http://books.rakuten.co.jp/rb/6531230/", 
                "largeImageUrl": "http://thumbnail.image.rakuten.co.jp/@0_mall/book/cabinet/8342/83426302.jpg?_ex=200x200", 
                "limitedFlag": 0, 
                "listPrice": 0, 
                "mediumImageUrl": "http://thumbnail.image.rakuten.co.jp/@0_mall/book/cabinet/8342/83426302.jpg?_ex=120x120", 
                "postageFlag": 0, 
                "publisherName": "\u30db\u30fc\u30e0\u793e\uff08\u5343\u4ee3\u7530\u533a\uff09", 
                "reviewAverage": "4.5", 
                "reviewCount": 2, 
                "salesDate": "2010\u5e7407\u6708", 
                "seriesName": "MANGA BUNGO\u30b7\u30ea\u30fc\u30ba", 
                "seriesNameKana": "\u30de\u30f3\u30ac \u30d6\u30f3\u30b4\u30a6 \u30b7\u30ea\u30fc\u30ba", 
                "size": "\u6587\u5eab", 
                "smallImageUrl": "http://thumbnail.image.rakuten.co.jp/@0_mall/book/cabinet/8342/83426302.jpg?_ex=64x64", 
                "subTitle": "", 
                "subTitleKana": "", 
                "title": "\u85ae\u306e\u4e2d", 
                "titleKana": "\u30e4\u30d6 \u30ce \u30ca\u30ab"
            }
        }
    ], 
    "carrier": 0, 
    "count": 129, 
    "first": 1, 
    "hits": 5, 
    "last": 5, 
    "page": 1, 
    "pageCount": 26
}
羅生門
芥川龍之介全集(2)
蜘蛛の糸
羅生門
薮の中
<!-- Rakuten Web Services Attribution Snippet FROM HERE -->
<a href="http://webservice.rakuten.co.jp/" target="_blank">Supported by 楽天ウェブサービス</a>
<!-- Rakuten Web Services Attribution Snippet TO HERE -->

お勧め書籍

Web Scraping with Python: Collecting Data from the Modern Web

Web Scraping with Python: Collecting Data from the Modern Web

PythonによるWebスクレイピング

PythonによるWebスクレイピング