1

I want to import the values from the following link: NSE

I have tried the following code:

function extract() {
  var html = UrlFetchApp.fetch('nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp?symbol=NIFTY').getContentText();
  var doc = XmlService.parse(html);
  var html = doc.getRootElement();
  var menu = getElementsByClassName(html, 'opttbldata')[0];
  var output = '';
  var linksInMenu = getElementsByTagName(menu, 'th');
  for(i in linksInMenu) output+= XmlService.getRawFormat().format(linksInMenu[i])+'<br>';
  return HtmlService.createHtmlOutput(output);
  //Logger.log(menu);
};


function getElementById(element, idToFind) {  
  var descendants = element.getDescendants();  
  for(i in descendants) {
    var elt = descendants[i].asElement();
    if( elt !=null) {
      var id = elt.getAttribute('id');
      if( id !=null && id.getValue()== idToFind) return elt;    
    }
  }
};


function getElementsByClassName(element, classToFind) {  
  var data = [];
  var descendants = element.getDescendants();
  descendants.push(element);  
  for(i in descendants) {
    var elt = descendants[i].asElement();
    if(elt != null) {
      var classes = elt.getAttribute('class');
      if(classes != null) {
        classes = classes.getValue();
        if(classes == classToFind) data.push(elt);
        else {
          classes = classes.split(' ');
          for(j in classes) {
            if(classes[j] == classToFind) {
              data.push(elt);
              break;
            }
          }
        }
      }
    }
  }
  return data;
};


function getElementsByTagName(element, tagName) {  
  var data = [];
  var descendants = element.getDescendants();  
  for(i in descendants) {
    var elt = descendants[i].asElement();     
    if( elt !=null && elt.getName()== tagName) data.push(elt);      
  }
  return data;
};

But I am getting the following error:
returned code 403. Truncated server response: Access Denied

Access Denied

You don't have permission to access "http://nseindia&#... (use muteHttpExceptions option to examine full response) (line 2, file "Code")
Rubén
  • 34,714
  • 9
  • 70
  • 166
jwilson
  • 81
  • 6

1 Answers1

3

That website checks the user agent to reject requests from anything that's not a browser. Unfortunately apps script will not allow you to change the user agent so you simply can't load that site with UrlFetchApp unless you manage to use a proxy server that allows you to fake your user agent.

Tesseract
  • 8,049
  • 2
  • 20
  • 37
  • Thanks! Can you please help me to achieve it (if it is possible) via using a proxy server? – jwilson Jul 30 '16 at 16:55
  • I don't know of any existing proxy that can be used from apps script and allows you to fake the user agent header. Do you or your company own a server of your own? In that case you could make it act as a proxy. Or maybe you can find another website that offers the same information as nseindia. Another solution would be to have some application running on a PC somewhere that periodically gets the data from nseindia and sends it over to your google drive. – Tesseract Jul 30 '16 at 17:24