我如何为数百个 URL 使用 IMPORTXML?

How Do I Use IMPORTXML For Hundreds of URL's?

基本上我有一排300+ URL的。

我需要在下一栏中提取每个 URL 旁边的标题。

所以我尝试了这个:

=IMPORTXML(A1,"//title")

并将其拖到 A300+ - 它有效,问题是它调用了太多东西 google 告诉我,我一直等到最后 2,然后不小心刷新了页面,再来一遍。

我也试过:

={IMPORTXML(A1,"//title");IMPORTXML(A327,"//title")}  

正如其他人所建议的那样,它仍在加载(同样的问题)。

有更好的方法吗?

不幸的是,IMPORTXML does not support ARRAYFORMULA。此外,当有数百个单元格包含 IMPORTXML 函数时,如果有多个 IMPORTXML 请求,则此函数的结果可能会重新加载。

备选方案

您可以通过 creating a bound script(见下文)在电子表格文件中试用此示例,该文件将为您提供自定义菜单:

此脚本将使用 UrlFetchApp 而不是 IMPORTXML 函数来获取 URL 的标题。

示例脚本:

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('URL titles')
      .addItem('Show URL titles on Column B', 'processTitles')
      .addToUi();
}

function processTitles(){
  var ss = SpreadsheetApp.getActive().getActiveSheet();
  var lastRow = ss.getDataRange().getLastRow();
  var url;
  
  for(row=1; row<= lastRow; row++){ //Script will start getting URL from A1 and beyond
    if(ss.getRange("B"+row).getValue() == ""){
        try{
          url = ss.getRange("A"+row).getValue();
          var response = UrlFetchApp.fetch(url).getContentText();
          ss.getRange("B"+row).setValue(response.match('<title>(.*?)</title>')[1])
          Logger.log(response.match('<title>(.*?)</title>')[1].toString());
          continue;
        }catch(e){
          ss.getRange("B"+row).setValue(response.match('title="(.*?)"')[1]);
          Logger.log(response.match('title="(.*?)"')[1].toString());
          continue;
        }
    }else{
      continue;
    }
  }
}

The custom menu Show URL titles on Column B will automate the scraping of URL titles by reading the HTML content of the URL and will look for the <title></title> tags OR title="" using the match() method. I've used the answer from https://whosebug.com/a/12030612 as a reference for this.

样本结果:

Sample sheet file with URLs on Column A:

Once you run the script from the editor, go back to your spreadsheet file and click the custom menu Show URL titles on Column B

Here's the result after running the custom menu: