0

I am trying to write a script to access the specific values that are displayed in the following line graph:

Line Grapg

Ideally, I want to get the last value (43) from the line graph. For that I tried to run the following sample code:

function W5(){

  var url ='https://trends.google.com/trends/explore?geo=US&q=%2Fm%2F07s_c';  
  var result = UrlFetchApp.fetch(url);
      
  const $ = Cheerio.load(result.getContentText());
  
  Logger.log($);
  return;
}

But it is giving me the following error message, due to which I am unable to parse any data and process it. It's strange because I have not made a single successful request even once but message says too many requests:

Exception: Request failed for https://trends.google.com returned code 429. Truncated server response: Error 429 (Too Many Req... (use muteHttpExceptions option to examine full response)

I followed this solution by changing the parameters according to my requirement:

function startQuery() {
  var sheet = ss.getSheetByName("Sheet2");

  // start the query
  var result = buildQueryString(sheet.getRange("A2").getValue(),
     sheet.getRange("B2").getValue());

  
  // display the resulting link in a cell
  sheet.getRange("C2").setValue(result);

  var csv_result = generateCsvDownload(sheet.getRange("A2").getValue(),
     sheet.getRange("B2").getValue());

  sheet.getRange("D2").setValue(csv_result);

}


function buildQueryString(geo,q) {
  return "http://www.google.com/trends/explore?" +
  "geo=" + encodeURIComponent(geo) +
  "&q=" + encodeURIComponent(q)
  
}


function generateCsvDownload(geo,q) {
  return "http://www.google.com/trends/viz?" +
 "geo=" + encodeURIComponent(geo) +
  "&q=" + encodeURIComponent(q) + 
  "&graph=all_csv";
}

This script takes parameters from the sheet table (starting from Column A):

geo q Query_Result CSV_Download_Link csv_result
US uneployement

But it just creates a link instead of actual CSV data output. Any help with that would be much appreciated. Thank you

  • 1
    If exponential backoff as mentioned in previous questions doesn't work, ask a new specific question. Note that it's not possible to change user agent or ip address. So, someone else might be spamming trends.google.com and Google might've blocked all apps script IPs(if not, a new project may help). – TheMaster Oct 28 '22 at 15:18
  • I tried to run it from different account, it shows the same error message –  Oct 28 '22 at 15:36
  • 1
    Related: https://stackoverflow.com/questions/22786068/how-to-avoid-http-error-429-too-many-requests-python https://stackoverflow.com/questions/62855667/urlfetchapp-fetch-throws-heavy-traffic-timeout-error – TheMaster Oct 28 '22 at 16:12

0 Answers0