You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python查询ISBN是否已关联对应书籍?

ISBN关联书籍查询解决方案

1. 调用结构化图书数据API

无需处理动态页面和验证码,直接通过专业API获取ISBN对应的图书信息,返回结果为结构化JSON,可靠性更高。

示例代码:

import requests

def is_isbn_associated(isbn: str) -> bool:
    """判断ISBN是否关联已有书籍"""
    params = {'q': f'isbn:{isbn}'}
    # 调用图书查询API
    response = requests.get('https://www.googleapis.com/books/v1/volumes', params=params)
    if response.status_code == 200:
        data = response.json()
        # 通过返回的条目数判断是否存在关联书籍
        return data.get('totalItems', 0) > 0
    return False

注意:这类API通常有调用频率限制,需遵守平台规则,避免触发限流。

2. 优化Selenium规避检测与动态加载

若坚持使用目标网站,可通过配置Selenium减少验证码触发,并等待页面动态加载完成:

示例代码:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

def check_isbn_via_selenium(isbn: str) -> bool:
    options = Options()
    # 无头模式+模拟真实浏览器UA
    options.headless = True
    options.add_argument('--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36')
    # 禁用自动化标识,避免被网站检测
    options.add_argument('--disable-blink-features=AutomationControlled')
    
    driver = webdriver.Firefox(options=options)
    try:
        driver.get(f'https://isbnsearch.org/isbn/{isbn}')
        # 等待页面核心内容加载,最长等待10秒
        WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.TAG_NAME, 'body'))
        )
        return "Not Found" not in driver.page_source
    finally:
        driver.quit()

说明:该方案仍存在触发验证码的风险,适合低频率查询场景,高频率使用建议搭配代理IP或验证码识别服务。

3. 本地ISBN数据库查询

若需离线使用,可下载公开的ISBN数据集(如Open Library的图书数据),导入本地数据库后直接查询,完全避免网络请求问题。需定期更新数据库以保证数据时效性。

内容的提问来源于stack exchange,提问作者Pedro Vinícius

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 10:57:35