簡體   English   中英

使用 Python 檢查 ISBN 是否已被使用

[英]Checking if a ISBN is already being used, with Python

我不想檢查 ISBN 是否有效

我試圖弄清楚,給定 ISBN isbn ,是否已經有一本書與之相關聯。

我已經嘗試過的

我打算使用以下 function:

import requests

def is_isbn_already_used(isbn:str)->bool:
    """ Given a ISBN string, returns True if there's a
    book already associated with it"""

    # Tries to search ISBN in the below site
    response = requests.get("https://isbnsearch.org/isbn/" + isbn)
    
    # checks if the site doesn't have any info about that isbn
    return not ("Not Found" in response.text)

問題是: function 獲取頁面文本的部分沒有按我預期的那樣工作。 可能是因為該站點是動態的,所以它會等待幾毫秒才能顯示有關該書的信息。

我正在考慮為 Firefox 處理 Selenium 的 Webdriver,但隨后我需要處理驗證碼。

任何想法將不勝感激。

好吧,我會回答我自己的問題:我決定使用 Google 圖書的 API。

值得注意的是,此 API 在搜索 ISBN 時是非嚴格的:搜索與任何圖書沒有正式關聯的 ISBN,例如 9780062059942,將返回 ISBN 與 9780062059942 密切相關的圖書:在本例中。

所以,我修改了我的 function:

from requests import get


def check_isbn_already_associated(isbn:str)->bool:
    """Returns True if this isbn is already associated with a book"""
    response = get("https://www.googleapis.com/books/v1/volumes?q=isbn:" + isbn)

    return isbn in response.text

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM