解析谷歌趋势 - JS对象

时间:2013-01-24 15:59:29

标签: javascript html parsing html-parsing

我正在尝试在上表中获取搜索字词及其值:

http://www.google.com/trends/explore#cat=0-14&date=today%207-d&cmpt=q

我无法确定他们所处的html标签/类/路径。我该如何解决这个问题?我尝试查看源代码,但没有多大帮助。

真的很感激任何帮助 - 谢谢!安托

1 个答案:

答案 0 :(得分:0)

以下代码段将返回一个数组数组(总体数组包含页面中每个表的值)。每个数组元素都有一个数组,其中每个表行都是一个对象,分解为它的'term'和'value'。

var tableValues = [];
var t = document.querySelectorAll(".trends-table-data");
if(t.length>0){
     var rows, row, cells, values;
     for(var i=0; i<t.length; i++){
          values = [];
          rows = t[i].getElementsByTagName("tr");
          for(var r=0; r<rows.length; r++){
               row = rows[r];
               if(row.className.indexOf('trends-table-row')===-1) continue;
               cells = row.getElementsByTagName("td");
               values.push({
                   term: cells[0].innerText.replace(/^\s+|\s+$/g, ''),
                   value: cells[1].innerText.replace(/^\s+|\s+$/g, '')
               });
          }
          tableValues[i] = values;
     }
     console.log(tableValues);
}

由于页面上有两个表,因此您引用的页面的输出为:

tableValues = [[{"term":"friv","value":"100"},{"term":"baby","value":"55"},{"term":"hot","value":"50"},{"term":"girls","value":"45"},{"term":"games","value":"45"},{"term":"juegos","value":"30"},{"term":"العاب","value":"25"},{"term":"love","value":"25"},{"term":"bible","value":"20"},{"term":"india","value":"20"}],[{"term":"sophiya haque","value":"Breakout"},{"term":"temple run 2","value":"+3,200%"},{"term":"крещение","value":"+700%"},{"term":"dear abby","value":"+450%"},{"term":"temple run","value":"+200%"},{"term":"amber heard","value":"+130%"},{"term":"plein champ","value":"+130%"},{"term":"paranormal activity 4","value":"+90%"},{"term":"scientology","value":"+70%"},{"term":"mama","value":"+60%"}]]