Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps javascript函数,用于返回搜索词的点击数_Javascript_Api_Search_Google Apps Script_Google Sheets - Fatal编程技术网

Google apps javascript函数,用于返回搜索词的点击数

Google apps javascript函数,用于返回搜索词的点击数,javascript,api,search,google-apps-script,google-sheets,Javascript,Api,Search,Google Apps Script,Google Sheets,我将如何实现一个用于返回谷歌搜索点击数的谷歌应用程序脚本?具体来说,我有一个谷歌电子表格,其中单元格A2中的公式是=GOOGLEHITS(A1) 我已经做到了: function GoogleHits (keywords) { return ??? ("\"" + keywords + "\"") ; } 想知道应该用什么替换“?”让我们假设您正在寻找一种从谷歌搜索页面中提取结果计数的方法。首先,找出页面中包含的位置。例如,在Firefox中,您可以使用Inspector(Inspect元素

我将如何实现一个用于返回谷歌搜索点击数的谷歌应用程序脚本?具体来说,我有一个谷歌电子表格,其中单元格A2中的公式是
=GOOGLEHITS(A1)

我已经做到了:

function GoogleHits (keywords) 
{
return ??? ("\"" + keywords + "\"") ;
}

想知道应该用什么替换“?”让我们假设您正在寻找一种从谷歌搜索页面中提取结果计数的方法。首先,找出页面中包含的位置。例如,在Firefox中,您可以使用Inspector(Inspect元素)来执行此操作

以前有过关于从网页解析信息的问题,使用UrlFetch和Xml服务很容易做到。请参考这些其他问题了解一些背景知识,以及简化解决方案的方便的dandy
getDivById()
函数

代码
GoogleHits
功能可以用作工作表中的自定义功能。test函数直接调用它

function test_GoogleHits() {
  function test( keywords ) {
    Logger.log( keywords + ' : ' + GoogleHits(keywords) );
  }

  test('googlehits');
  test('pizza');
  test('today\'s weather' );
}

function GoogleHits(keywords) {
  var target = "https://www.google.ca/search?q="+encodeURI(keywords);

  var pageTxt = UrlFetchApp.fetch(target).getContentText();
  var pageDoc = Xml.parse(pageTxt,true);
  var contentDiv = getDivById( pageDoc.getElement().body, 'resultStats' );
  return extractInteger( contentDiv.Text );
}

function extractInteger(str) {
  var num = "";
  var inNum = false;
  for(var i=0; i<str.length; i++) {
    var c = str.charAt(i);
    if (c>='0' && c<= '9') {
      if (!inNum) inNum = true;
      num += c;
    }
    else if (c !== ',') {
      if (inNum) break;
    }
  }
  return parseInt(num);
}
功能测试_GoogleHits(){
功能测试(关键字){
Logger.log(关键字+':'+GoogleHits(关键字));
}
测试(“谷歌点击”);
测试(“比萨饼”);
测试(“今天的天气”);
}
功能谷歌点击(关键字){
变量目标=”https://www.google.ca/search?q=“+encodeURI(关键字);
var pageTxt=UrlFetchApp.fetch(目标).getContentText();
var pageDoc=Xml.parse(pageTxt,true);
var contentDiv=getDivById(pageDoc.getElement().body,'resultStats');
返回extractInteger(contentDiv.Text);
}
函数提取整数(str){
var num=“”;
var inNum=假;
对于(变量i=0;i='0'&&c