【问题标题】:How to scrape images with Cheerio and paste to Google Sheets?如何使用 Cheerio 抓取图像并粘贴到 Google 表格?
【发布时间】:2021-11-27 21:24:11
【问题描述】:

这是我第一次尝试学习如何从网络上抓取图像并将其粘贴到 Google 表格中。我想从https://ir.eia.gov/ngs/ngs.html 下载第二张图片并将其粘贴到 Google 表格中。在网络上,有两个图像。我想获得 下的第二张图像。我喜欢学习如何在代码中引用它的 img alt= 或 src="ngs.gif" 而不是索引,因此我也可以将这个概念用于其他各种 HTML 情况。任何人都可以帮助修复以下代码以便我学习吗?谢谢!

function test() {
  const url = 'https://ir.eia.gov/ngs/ngs.html';
  const res = UrlFetchApp.fetch(url, { muteHttpExceptions: true }).getContentText();
  var $ = Cheerio.load(res);
  
  // I want to download the image, <img alt="Working Gas in Underground Storage Compared with Five-Year Range" src="ngs.gif" border="0">
  // What should be changed in the following code?
  var chart = $('img').attr('src').find('ngs.gif');
  SpreadsheetApp.getActiveSheet().insertImage(chart, 1, 1);
}

【问题讨论】:

  • Cheerio 似乎无法做到这一点,因为它只会返回src 的文件名,而不是ngs.gif 图像文件的完整源路径链接。您需要使用 getBlob()ngs.gif 图像的实际源路径 URL 作为 blob 获取,然后您可以轻松地将其作为图像插入到工作表中。

标签: image google-apps-script web-scraping cheerio


【解决方案1】:

我相信你的目标如下。

  • 您想检索img 标签的第二张图片并将其放入电子表格。

在这个 HTML 中,URL 似乎是 https://ir.eia.gov/ngs/ + filename。所以我认为可以使用insertImage(url, column, row)的方法。当这反映到您的脚本中时,以下修改后的脚本怎么样?

修改脚本:

function test() {
  const url = 'https://ir.eia.gov/ngs/ngs.html';
  const res = UrlFetchApp.fetch(url, { muteHttpExceptions: true }).getContentText();
  const $ = Cheerio.load(res);
  const urls = [];
  $('img').each(function () {
    urls.push("https://ir.eia.gov/ngs/" + $(this).attr('src'));
  });
  if (urls.length > 1) {
    SpreadsheetApp.getActiveSheet().insertImage(urls[1], 1, 1); // 2nd image is retrieved.
  }
}
  • 运行此脚本时,将检索https://ir.eia.gov/ngs/ngs.gif 的 URL,并将图像放入电子表格。

参考:

补充:

关于您在评论中的以下新问题,

非常感谢!那么除了调用图像的索引之外,代码中是否没有方法可以调用alt="地下存储工作气体与五年范围比较"或src="ngs.gif"?我只是想了解一种潜在场景的智能方法,例如,如果网络有 20 张图像,并且这些图像的位置每天都在变化,那么第二张图像并不总是排在第二位。再次感谢您的任何指导!

在这种情况下,下面的示例脚本怎么样?

示例脚本:

function test() {
  const url = 'https://ir.eia.gov/ngs/ngs.html';
  const res = UrlFetchApp.fetch(url, { muteHttpExceptions: true }).getContentText();
  const $ = Cheerio.load(res);

  const obj = [];
  $('img').each(function () {
    const t = $(this);
    const src = t.attr('src');
    obj.push({ alt: t.attr('alt'), src: src, url: "https://ir.eia.gov/ngs/" + src });
  });

  const searchAltValue = "Working Gas in Underground Storage Compared with Five-Year Range";
  const searchSrcValue = "ngs.gif";
  const ar = obj.filter(({alt, src}) => alt == searchAltValue && src == searchSrcValue);
  if (ar.length > 0) {
    SpreadsheetApp.getActiveSheet().insertImage(ar[0].url, 1, 1);
  }
}
  • 在此示例脚本中,当srcalt 的值分别为Working Gas in Underground Storage Compared with Five-Year Rangengs.gif 时,将检索URL 并将其放入图像中。
  • 如果要选择Working Gas in Underground Storage Compared with Five-Year Rangengs.gif,请将alt == searchAltValue &amp;&amp; src == searchSrcValue修改为alt == searchAltValue || src == searchSrcValue

【讨论】:

  • 非常感谢!那么除了调用图像的索引之外,代码中是否没有方法可以调用alt="地下存储工作气体与五年范围比较"或src="ngs.gif"?我只是想了解一种潜在场景的智能方法,例如,如果网络有 20 张图像,并且这些图像的位置每天都在变化,那么第二张图像并不总是排在第二位。再次感谢您的任何指导!
  • @Newbie 感谢您的回复。我很高兴你的问题得到了解决。关于您的新问题,我在回答中又添加了一个示例脚本。你能确认一下吗?如果我误解了您的新问题,我深表歉意。
  • 你添加的脚本太酷了。这正是我想学的。非常感谢!
  • @Newbie 感谢您的回复。我很高兴你的问题得到了解决。也谢谢你。
猜你喜欢
  • 2020-07-15
  • 2023-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多