{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "import Requests" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import requests" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "http://zipcloud.ibsnet.co.jp/api/search にアクセス。パラメータ\"zipcode\"に郵便番号を渡すと、その住所情報を得ることができる。" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [], "source": [ "payload = {'zipcode': 1600022}\n", "r = requests.get('http://zipcloud.ibsnet.co.jp/api/search', params=payload)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "status_codeで、レスポンスのステータスを表示" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "200" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r.status_code" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "レスポンスのヘッダーを表示" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{'alternate-protocol': '80:quic,p=0.01,80:quic,p=0.01', 'transfer-encoding': 'chunked', 'vary': 'Accept-Encoding', 'server': 'Google Frontend', 'connection': 'Keep-Alive', 'cache-control': 'private', 'date': 'Sat, 27 Sep 2014 17:31:01 GMT', 'content-type': 'text/plain; charset=utf-8'}" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r.headers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "レスポンスのエンコードを表示" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'utf-8'" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r.encoding" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "レスポンスの中身を表示。Requestsの内部で自動でユニコードに変換してくれる。" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "u'{\\n\\t\"message\": null,\\n\\t\"results\": [\\n\\t\\t{\\n\\t\\t\\t\"address1\": \"\\u6771\\u4eac\\u90fd\",\\n\\t\\t\\t\"address2\": \"\\u65b0\\u5bbf\\u533a\",\\n\\t\\t\\t\"address3\": \"\\u65b0\\u5bbf\",\\n\\t\\t\\t\"kana1\": \"\\uff84\\uff73\\uff77\\uff6e\\uff73\\uff84\",\\n\\t\\t\\t\"kana2\": \"\\uff7c\\uff9d\\uff7c\\uff9e\\uff6d\\uff78\\uff78\",\\n\\t\\t\\t\"kana3\": \"\\uff7c\\uff9d\\uff7c\\uff9e\\uff6d\\uff78\",\\n\\t\\t\\t\"prefcode\": \"13\",\\n\\t\\t\\t\"zipcode\": \"1600022\"\\n\\t\\t}\\n\\t],\\n\\t\"status\": 200\\n}'" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r.text" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "表示してみる。" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", "\t\"message\": null,\n", "\t\"results\": [\n", "\t\t{\n", "\t\t\t\"address1\": \"東京都\",\n", "\t\t\t\"address2\": \"新宿区\",\n", "\t\t\t\"address3\": \"新宿\",\n", "\t\t\t\"kana1\": \"トウキョウト\",\n", "\t\t\t\"kana2\": \"シンジュクク\",\n", "\t\t\t\"kana3\": \"シンジュク\",\n", "\t\t\t\"prefcode\": \"13\",\n", "\t\t\t\"zipcode\": \"1600022\"\n", "\t\t}\n", "\t],\n", "\t\"status\": 200\n", "}\n" ] } ], "source": [ "print r.text" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "jsonでロードして、値を取り出してみる。" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "東京都 新宿区 新宿\n" ] } ], "source": [ "import json\n", "issue = json.loads(r.text)\n", "print issue['results'][0]['address1'], \\\n", " issue['results'][0]['address2'], \\\n", " issue['results'][0]['address3']" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 0 }