问题描述
我想获取某些域的索引页数。因此我想使用“site:”参数并从搜索结果页面中提取结果数量。
我使用 Google 电子表格的 Google-apps-script 进行了尝试:
function sampleFormula_4() {
const url = "https://www.google.com/search?q=site%3Abenedikt-sahlmueller.de";
try {
const html = UrlFetchApp.fetch(url).getContentText();
return html.match(/<div id="result-stats">(.+?)nobr>/)[1].trim();
} catch (e) {
Utilities.sleep(5000);
const html = UrlFetchApp.fetch(url).getContentText();
return html.match(/<div id="result-stats">(.+?)nobr>/)[1].trim();
}
}
Google 电子表格显示错误 429 - 请求过多。我集成了 5000 毫秒的睡眠时间,但 Google 搜索仍然返回错误 429。
我需要的只是 Google 搜索结果中某些网址的页数。也许有更好的方法 - 我不能为此使用 search-api,因为这些页面不是我的 GSC 的一部分。
解决方法
很可能 Google 搜索将来自 UrlFetch
的请求视为自动流量并因此阻止它们。来自the official docs:
Google 认为什么是自动流量
- 从机器人、计算机程序、自动化服务或搜索抓取工具发送搜索
例如,使用 wget
或 curl
等工具时会发生相同的行为。
建议使用 Search API。