使用UrlFetchApp在Google Apps脚本中提取表 [英] Extracting a table in Google Apps Script using UrlFetchApp

查看:162
本文介绍了使用UrlFetchApp在Google Apps脚本中提取表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从以下链接导入值: NSE

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

我尝试了以下代码:

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;
};

但是出现以下错误:
返回代码403.服务器响应被截断:访问被拒绝

访问被拒绝

您无权访问"http://nseindia&#...(使用MutantHttpExceptions选项检查完整响应)(第2行) ,文件代码")

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")

推荐答案

该网站检查用户代理以拒绝来自非浏览器的请求.不幸的是,应用程序脚本不允许您更改用户代理,因此您无法使用UrlFetchApp加载该站点,除非您设法使用允许您伪造用户代理的代理服务器.

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.

这篇关于使用UrlFetchApp在Google Apps脚本中提取表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆