[英]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.