简体   繁体   English

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

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

I don't want to check if a ISBN is valid .我不想检查 ISBN 是否有效

I'm trying to figure out if, given a ISBN isbn , there's already a book associated with it.我试图弄清楚,给定 ISBN isbn ,是否已经有一本书与之相关联。

What I already tried我已经尝试过的

I was planning to use the following function:我打算使用以下 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)

The problem is: The part where the function gets the text of the page doesn't works as I expected.问题是: function 获取页面文本的部分没有按我预期的那样工作。 Probably, because the site is dynamic, it waits a few ms before showing the info about the book.可能是因为该站点是动态的,所以它会等待几毫秒才能显示有关该书的信息。

I was thinking of dealing with Selenium's Webdriver for Firefox, but then I would need to deal with the captcha.我正在考虑为 Firefox 处理 Selenium 的 Webdriver,但随后我需要处理验证码。

Any idea would be appreciated.任何想法将不胜感激。

Well, I'll answer my own question: I decided to use the Google Books' API.好吧,我会回答我自己的问题:我决定使用 Google 图书的 API。

Worth noting that this API is non-strict when it searches for ISBNs: Searching for a ISBN that is not officially associated with any book, like 9780062059942, will return a book with an ISBN closely associated with 9780062059942: The Selection, in this case.值得注意的是,此 API 在搜索 ISBN 时是非严格的:搜索与任何图书没有正式关联的 ISBN,例如 9780062059942,将返回 ISBN 与 9780062059942 密切相关的图书:在本例中。

So, I modified my function:所以,我修改了我的 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