This is the accompanying code file for the video Coding With Python :: Learn API Basics to Grab Data with Python.
Check this Google Books Developer link for some code samples.
This whole article uses ISBNdb for URL searches. Open Library is also mentioned as an API source but not used in any of the code.
ISBNdb:
1curl http://isbndb.com/api/v2/json/RK8FPFZU/book/0765350386
Open Library:
1curl 'http://openlibrary.org/api/books?bibkeys=ISBN:0201558025&jscmd=data&format=json'
For
1curl http://isbndb.com/api/v2/json/RK8FPFZU/book/0765350386
1{
2 "data" : [
3 {
4 "isbn10" : "0765350386",
5 "title_long" : "",
6 "notes" : "",
7 "awards_text" : "",
8 "subject_ids" : [],
9 "isbn13" : "9780765350381",
10 "urls_text" : "",
11 "book_id" : "mistborn",
12 "physical_description_text" : "672 pages",
13 "summary" : "Brandon Sanderson, fantasy�s newest master tale spinner, author of the acclaimed debut Elantris, dares to turn a genre on its head by asking a simple question: What if the hero of prophecy fails? What kind of world results when the Dark Lord is in charge? The answer will be found in the Mistborn Trilogy, a saga of surprises and magical martial-arts action that begins in Mistborn.For a thousand years the ash fell and no flowers bloomed. For a thousand years the Skaa slaved in misery and lived in fear. For a thousand years the Lord Ruler, the �Sliver of Infinity,� reigned with absolute power and ultimate terror, divinely invincible. Then, when hope was so long lost that not even its memory remained, a terribly scarred, heart-broken half-Skaa rediscovered it in the depths of the Lord Ruler�s most hellish prison. Kelsier �snapped� and found in himself the powers of a Mistborn. A brilliant thief and natural leader, he turned his talents to the ultimate caper, with the Lord Ruler himself as the mark. Kelsier recruited the underworld�s elite, the smartest and most trustworthy allomancers, each of whom shares one of his many powers, and all of whom relish a high-stakes challenge. Only then does he reveal his ultimate dream, not just the greatest heist in history, but the downfall of the divine despot.But even with the best criminal crew ever assembled, Kel�s plan looks more like the ultimate long shot, until luck brings a ragged girl named Vin into his life. Like him, she�s a half-Skaa orphan, but she�s lived a much harsher life. Vin has learned to expect betrayal from everyone she meets, and gotten it. She will have to learn to trust, if Kel is to help her master powers of which she never dreamed.Readers of Elantris thought they'd discovered someone special in Brandon Sanderson. Mistborn proves they were right.",
14 "dewey_normal" : "813",
15 "publisher_name" : "Tor Fantasy",
16 "edition_info" : "Mass Market Paperback; 2007-07-31",
17 "language" : "",
18 "lcc_number" : "",
19 "publisher_text" : "Tor Fantasy",
20 "publisher_id" : "tor_fantasy",
21 "title" : "Mistborn",
22 "title_latin" : "Mistborn",
23 "marc_enc_level" : "~",
24 "dewey_decimal" : "813",
25 "author_data" : [
26 {
27 "name" : "Brandon Sanderson",
28 "id" : "brandon_sanderson"
29 }
30 ]
31 }
32 ],
33 "index_searched" : "isbn"
34}
1#!/usr/bin/env python
2
3import urllib2
4import json
5
6url = 'http://isbndb.com/api/v2/json/RK8FPFZU/book/0765350386'
7obj = urllib2.urlopen(url)
8response = json.load(obj)
9
10for book in response['data']:
11 print book['title']
12 for author in response['data'][0]['author_data']:
13 print author['name']
14 print book['physical_description_text']
Mistborn
Brandon Sanderson
672 pages
We will use urllib2
to open the URL and json
to parse the json data the URL provides.
1#!/usr/bin/env python
2
3import urllib2
4import json
5
6# URL Structure for ISBNDB JSON API
7# http://isbndb.com/api/v2/json/api_key/book/isbn
8
9# ISBNDB_key = 'RK8FPFZU'
10# isbn = '0743277465'
11
12url = 'http://isbndb.com/api/v2/json/RK8FPFZU/book/0743277465'
13
14obj = urllib2.urlopen(url)
15
16response = json.load(obj)
17
18print response
1{
2 "index_searched" : "isbn",
3 "data" : [
4 {
5 "lcc_number" : "GV1439",
6 "marc_enc_level" : "7",
7 "publisher_text" : "New York : Free Press, 2008.",
8 "urls_text" : "",
9 "book_id" : "the_art_of_learning_a04",
10 "publisher_name" : "Free Press",
11 "language" : "eng",
12 "title_latin" : "art of learning",
13 "awards_text" : "",
14 "subject_ids" : [
15 "entertainment_puzzles_games_board_games_chess",
16 "health_mind_body_self_help_motivational",
17 "health_mind_body_personal_health",
18 "health_mind_body_psychology_counseling_by_topic_learning",
19 "biographies_memoirs_memoirs",
20 "waitzkin_josh",
21 "chess_players_united_states_biography",
22 "tai_chi",
23 "learning",
24 "meditation"
25 ],
26 "dewey_normal" : "613.7148",
27 "author_data" : [
28 {
29 "id" : "josh_waitzkin",
30 "name" : "Josh Waitzkin"
31 }
32 ],
33 "title_long" : "The art of learning: an inner journey to optimal performance",
34 "dewey_decimal" : "613.7/148",
35 "summary" : "",
36 "isbn10" : "0743277465",
37 "isbn13" : "9780743277464",
38 "physical_description_text" : "xxi, 265 p. : ill. ; 22 cm.",
39 "notes" : "The foundation -- My second art -- Bringing it all together.",
40 "edition_info" : "",
41 "publisher_id" : "free_press",
42 "title" : "The art of learning"
43 }
44 ]
45}
printing items from lists (iterating through lists)
1for book in response['data']:
2 print book['title']
3 for author in response['data'][0]['author_data']:
4 print author['name']
5 print book['physical_description_text']
Be sure to read this and this for iterating over ’lists in an object’ like we have the ‘author_data’ list in the ‘data’ object.
For the purpose of this article i’m only getting the title, author and number of pages. You can edit the code to output as many details as you want from the data object.
1#!/usr/bin/env python
2
3import urllib2
4import json
5
6isbndb_key = 'RK8FPFZU'
7
8def isbn_search(query):
9 api_key = isbndb_key
10 url = 'http://isbndb.com/api/v2/json/' + api_key + '/book/'
11 isbn = query
12 # isbn = query.replace(' ', '%20') will replace any spaces with %20.
13 # not needed in this url design/structure though so commented out
14 final_url = url + query
15
16 obj = urllib2.urlopen(final_url)
17 response = json.load(obj)
18
19 for book in response['data']:
20 print book['title']
21 for author in response['data'][0]['author_data']:
22 print author['name']
23 print book['physical_description_text']
For isbn_search('0765350386')
Mistborn
Brandon Sanderson
672 pages
For isbn_search('0743277465')
The art of learning
Josh Waitzkin
xxi, 265 p. : ill. ; 22 cm.
The pages are actually taken from the physical_description_text item, so it is okay for it to sometimes show other physical attributes in addition to the pages. If this bothers you, use the Open Library API that gives out number_of_pages insated of physical_description_text. The code is just an example.
1import urllib2
2import json
3
4ISBNDB_key = 'RK8FPFZU'
5
6def name_search(query):
7 api_key = ISBNDB_key
8 url = 'http://isbndb.com/api/v2/json/' + api_key + '/book/'
9 name = query.replace(' ', '_') #replace any spaces with _
10 final_url = url + name
11 obj = urllib2.urlopen(final_url)
12 response = json.load(obj)
13
14 for book in response['data']:
15 print book['title']
16 for author in response['data'][0]['author_data']:
17 print author['name']
18 print book['physical_description_text']
For name_search('mistborn')
Mistborn
Brandon Sanderson
672 pages
For name_search('the girl who kicked the hornets nest')
The Girl Who Kicked the Hornets' Nest
Stieg Larsson
576 pages