pythongoogle-apiisbngoogle-books-api

Issues retrieving books by ISBN using Google Books API


I’m building my own “online library” app and I’m retrieving book data using the Google Books API. The API works in general, but I’m running into issues specifically when searching by ISBN.

For most books I’ve tried, searching by ISBN returns no results. However, if I use the Google Books book ID, the book is found correctly.

I’d like to rely on ISBN search — for example, by scanning a barcode — but right now that approach is unreliable. Has anyone experienced this issue or knows a workaround?

Any help would be appreciated!

P.S. I also tried the Open Library API and encountered the same issue with ISBNs returning no results.

The function that I'm using:

def search_for_books(isbn, max_results=3):
    if not isinstance(max_results, int):
        max_results = 3
    API_ENDPOINT = 'https://www.googleapis.com/books/v1/volumes'
    params = {'q': f'isbn:{isbn}', 'maxResults': max_results}
    try:
        res = requests.get(API_ENDPOINT, params=params)
        return res.json()
    except requests.exceptions.RequestException:
        return []

Output when a ISBN is not found:

{'kind': 'books#volumes', 'totalItems': 0}

Solution

  • This works for me:

    import requests
    from isbnlib import to_isbn13, to_isbn10
    
    def search_for_books(isbn): 
        API_ENDPOINT = 'https://www.googleapis.com/books/v1/volumes'
    
        headers = {
            "User-Agent": "YOUR_BROWSER_USER_AGENT_STRING", 
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
            "Accept-Language": "en-US,en;q=0.5",
            "Connection": "keep-alive",
            "Upgrade-Insecure-Requests": "1",
        }
    
        queries = [isbn]
        isbn13 = to_isbn13(isbn)
        if isbn13 and isbn13 != isbn:
            queries.append(isbn13)
        isbn10 = to_isbn10(isbn)
        if isbn10 and isbn10 != isbn:
            queries.append(isbn10)
    
        for query_isbn in queries:
            params = {'q': f'isbn:{query_isbn}'} 
            try:
                print(f"Trying ISBN: {query_isbn}")
                res = requests.get(API_ENDPOINT, params=params, headers=headers)
                # print(f"URL: {res.url}")
                # print(f"Status Code: {res.status_code}")
                # print(16*'#')
                # print(f"Response JSON: {res.json()}")
                data = res.json()
                if 'items' in data:
                    return data
            except requests.exceptions.RequestException as e:
                print(f"Request error: {e}")
    
        return {'items': []}
    
    result = search_for_books("3642388906")
    if result and 'items' in result and result['items']:
        book_info = result['items'][0]['volumeInfo']
        print(f"Title: {book_info.get('title')}")
        print(f"Authors: {book_info.get('authors')}")
        print(f"Publisher: {book_info.get('publisher')}")
        print(f"Description: {book_info.get('description')}")
        # ... and so on for other relevant fields
    
    else:
        print("No books found.")
    

    Output:

    Trying ISBN: 3642388906
    Title: Dubbel
    Authors: ['Karl-Heinrich Grote', 'Jörg Feldhusen']
    Publisher: Springer Vieweg
    Description: 100 Jahre DUBBEL 1914 erschien die erste Auflage des Taschenbuch für den Maschinenbau, herausgegeben von Heinrich Dubbel. Seitdem ist der DUBBEL das Standardwerk der Ingenieure in Studium und Beruf mit den Schwerpunkten „Allgemeiner Maschinenbau“ sowie „Verfahrens- und Systemtechnik“. Die laufende Neubearbeitung garantiert die Dokumentation des aktuellen Stands der Technik. Dieses etablierte Referenzwerk mit „Norm-Charakter“ überzeugt durch - detaillierte Konstruktionszeichnungen - Tabellen und Diagramme mit quantitativen Angaben - Berechnungsverfahren - ein umfangreiches Literaturverzeichnis Der DUBBEL stellt das erforderliche Basis- und Detailwissen des Maschinenbaus zur Verfügung. Für die Jubiläumsauflage wurden alle Kapitel aktualisiert. Neu hinzugekommen ist die Medizintechnik, die fertigungstechnischen Kapitel wurden stark überarbeitet. Auch erhalten die Leser des Werkes Zugang zur MDesign Formelsammlung. Die ausführliche Darstellung der Mathematik ist als DUBBEL Mathematik separat erhältlich.