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

如何用Google Sheets/App Script抓取WorldCat动态页面的图书数据?

在Google Sheets中抓取WorldCat图书数据的解决方案

因为WorldCat页面依赖JavaScript渲染,ImportHTML/ImportXML这类内置函数无法获取动态加载的内容,推荐使用WorldCat官方Search API结合Google Apps Script实现,以下是针对新手的分步实现方案:

步骤1:获取WorldCat API密钥

前往OCLC开发者平台注册账号,创建新应用后获取专属的WSKey(即API密钥)。

步骤2:编写Apps Script代码

打开你的Google Sheets,点击「扩展程序」→「Apps 脚本」,清空默认代码后粘贴以下内容:

function GETWORLDDATA(bookTitle) {
  if (!bookTitle) return "请输入书名";
  
  // 替换为你申请到的WSKey
  const API_KEY = "YOUR_API_KEY";
  const BASE_API_URL = "https://www.worldcat.org/webservices/catalog/search/worldcat";
  
  // 编码书名,避免URL格式错误
  const encodedTitle = encodeURIComponent(bookTitle);
  const requestUrl = `${BASE_API_URL}?q=${encodedTitle}&format=json&wskey=${API_KEY}&count=1`;
  
  try {
    // 发起API请求并解析返回的JSON数据
    const response = UrlFetchApp.fetch(requestUrl);
    const jsonData = JSON.parse(response.getContentText());
    
    // 获取第一本匹配的图书记录
    const targetBook = jsonData.searchResults?.record?.[0];
    if (!targetBook) return "未找到匹配的图书";
    
    // 提取所需字段,处理字段不存在的情况
    const author = targetBook.author?.[0]?.value || "未知作者";
    const genre = targetBook.genre?.[0]?.value || "未知类型";
    const pubDate = targetBook.date?.[0]?.value || "未知出版日期";
    const coverImage = targetBook.coverImage?.[0]?.value || "无封面链接";
    
    // 返回数组,自动填充到当前单元格右侧的4列中
    return [[author, genre, pubDate, coverImage]];
    
  } catch (error) {
    return `错误信息: ${error.message}`;
  }
}

步骤3:在表格中使用自定义函数

  1. 保存脚本项目(命名任意,比如「WorldCat图书抓取」),回到Google Sheets界面。
  2. 在单元格中输入书名(比如A1单元格输入Remarkable Creatures)。
  3. 在B1单元格输入公式:=GETWORLDDATA(A1),按回车后,B1-E1单元格会自动填充作者、类型、出版日期、封面链接。

注意事项

  • API调用有免费限额,避免短时间内大量请求;
  • 若API返回的数据结构调整,需对应修改代码中字段提取的逻辑;
  • 书名输入越精准,匹配结果越准确。

内容的提问来源于stack exchange,提问作者Little Buddy

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 14:14:59