为什么 UrlFetchApp.fetch(url).getContentText() 返回不一致的结果?

问题描述

我正在遍历亚马逊产品列表,以获取列表中每个产品的负面评论。 但是,大约 90% 的情况下,UrlFetchApp.fetch("url").getContentText(); 将返回 HTML 的简短版本,其中不包含页面上的任何实际内容

现在我正在强制循环一次又一次地尝试,直到返回正确的(更长的)HTML,但这需要很长时间。我想知道是否有人知道任何替代方案或任何技巧以使其更加一致?

例如,以下是其中一个 URL:(网页的动态特性是否与不一致有关?)

https://www.amazon.com/product-reviews/B00J2DGTD8/ref=cm_cr_arp_d_viewopt_srt?ie=UTF8&filterByStar=one_star&reviewerType=all_reviews&pageNumber=1&sortBy=recent#reviews-filter-bar

这是一个最小的可重复示例:

function mre() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var lastRow = sheet.getLastRow();

  var star = ["one","two","three"];

  for(var a = 0; a < 3; a++) { //star 

    htmlRaw = UrlFetchApp
      .fetch(`https://www.amazon.com/product-reviews/B00J2DGTD8/ref=cm_cr_arp_d_viewopt_srt?ie=UTF8&filterByStar=${star[a]}_star&reviewerType=all_reviews&pageNumber=1&sortBy=recent#reviews-filter-bar`).getContentText(); 

    html = htmlRaw.toString();  
  
    //Logger.log(htmlRaw);
    //Logger.log(html);

    //Making sure we have the correct HTML
    //The below if statement acts like a gate. It tries again and again until it receives the correct version of the HTML. Then it will "open the gate" to the else{} below
  
    if(html.substring(100000,100500) == ``) { //the longer (correct) version of the HTML has 100,000+ characters
      
      Logger.log(`if its empty,then try again: ${html.substring(100000,100500)}`);
      a--;
    }

    else {

      lastRow = sheet.getLastRow(); //so we actually move the down the rows
      sheet.getRange(lastRow+1,1).setValue(html.substring(100000,100500)); //html snippet

    } //else (correct html)
  } //for (a - stars)
} //function mre()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)