||

如何获得谷歌搜索下拉框关键字?

出海品牌数字营销者们在做关键字研究时需要用许多工具,其中一个重要的数据源便是谷歌搜索的相关词。而谷歌检索词自动完成,即搜索下拉框,是一个高质量的相关词数据源。那么如何自制工具批量获取该数据呢?马老师本篇将给出解决方案。

谷歌搜索关键字自动完成提示

我们将通过Google Sheets来制作工具,如果你不想往下再读了,可以直接通过这个链接创建一个副本。

https://docs.google.com/spreadsheets/d/1IVW6Cf0PCxMOWTCU71_uJuJIxxGZ7KhSI8nZvRNjUyE/edit?usp=sharing

工具使用界面

使用方法很简单:

  1. 在A列输入你要查询的检索词
  2. 选中你要查询的检索词
  3. 在菜单中点击Autocomplete Suggestions >Print Suggestions for Range

记得跑的时候要用你的谷歌账号授权并且对unsafe的脚本继续推进。

接下来讲如何自己做一个工具。

  1. 新建一个Google Sheet,自己在第一行添加一个表头。
  2. 在Extensions中打开Apps Script。
  3. 把下面代码复制到Code.gs中并保存。
  4. 关闭后刷新Google Sheet。完成。

代码如下,还不到一百行:

function getGoogleAutocompleteSuggestions(keyword) {
  try {
    // Construct the URL for the API request
    const apiUrl = `https://www.google.com/complete/search?q=${encodeURIComponent(keyword)}&cp=11&client=gws-wiz&xssi=t&gs_pcrt=undefined&hl=en&authuser=0`;
    // Make the HTTP GET request
    const response = UrlFetchApp.fetch(apiUrl);
    let content = response.getContentText();
    // Clean up the response to remove invalid JSON prefix ")]}'\n"
    content = content.replace(/^\)\]\}'/, "").trim();
    // Parse the cleaned content into a JSON object
    const json = JSON.parse(content);
    // Extract the suggestions
    const suggestions = json[0].map(item => {
      // Decode unicode characters and remove HTML tags
      return item[0]
        .replace(/\\u([0-9a-fA-F]{4})/g, (_, code) => String.fromCharCode(parseInt(code, 16)))
        .replace(/<\/?[^>]+(>|$)/g, ""); // Strip HTML tags
    });
    return suggestions;
  } catch (error) {
    console.error("Error fetching or processing autocomplete suggestions:", error);
    return [];
  }
}
function printSuggestion(keywordCell, startCell) {
  // Get the active sheet
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  // Get the keyword from the input cell
  var keyword = sheet.getRange(keywordCell).getValue();
  // Call the getGoogleAutocompleteSuggestions function to fetch suggestions
  var suggestions = getGoogleAutocompleteSuggestions(keyword);
  // If suggestions are found, print them horizontally starting from the specified cell
  if (suggestions.length > 0) {
    var range = sheet.getRange(startCell).offset(0, 0, 1, suggestions.length);
    range.setValues([suggestions]);  // Set the suggestions in a single row
  } else {
    Logger.log("No suggestions found.");
  }
}
// Function to print suggestions for each keyword in the selected range
function printSuggestionsForRange() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  // Get the currently selected range (vertical range of keywords)
  var selection = sheet.getSelection();
  var range = selection.getActiveRange();
  var keywords = range.getValues(); // This returns a 2D array
  // Get the first cell to the right of the selected range (ensure it is within bounds)
  var firstRow = range.getRow();  // The first row of the selection
  var firstColumn = range.getColumn();  // The first column of the selection
  var lastRow = range.getLastRow(); // The last row of the selection
  // Get the next column to the right of the selected range
  var nextColumn = firstColumn + 1;
  var numColumns = sheet.getMaxColumns();
  if (nextColumn > numColumns) {
    // If out of bounds, alert the user
    Logger.log("Error: The selection is at the edge of the sheet. There is no next column.");
    return;
  }
  // For each keyword in the range, call the printSuggestion function
  for (var i = 0; i < keywords.length; i++) {
    var keyword = keywords[i][0]; // Get the keyword value from the row
    var currentOutputCell = sheet.getRange(firstRow + i, nextColumn); // Output starts in the next column in the same row
    // Call printSuggestion to print suggestions horizontally
    printSuggestion(keyword, currentOutputCell.getA1Notation());
  }
}
// Modified printSuggestion to work with cell range
function printSuggestion(keyword, startCell) {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var suggestions = getGoogleAutocompleteSuggestions(keyword); // Fetch suggestions
  if (suggestions.length > 0) {
    // Create a range starting from the given startCell and set the values
    var range = sheet.getRange(startCell).offset(0, 0, 1, suggestions.length);
    range.setValues([suggestions]); // Print suggestions in a horizontal row
  } else {
    Logger.log("No suggestions found for: " + keyword);
  }
}
// Function to add a custom menu to the sheet
function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Autocomplete Suggestions')
    .addItem('Print Suggestions for Range', 'menuPrintSuggestionsForRange')
    .addToUi();
}
// Menu function to call printSuggestionsForRange
function menuPrintSuggestionsForRange() {
  printSuggestionsForRange();
}

以上就是工具的整个制作过程。如果你有疑问请公众号后台私信。

类似文章