用google-trends-api 做谷歌趋势分析(NODEJS版)

简介:

Google Trends大家应该比较熟悉了,它可以查询特定关键字在不同国家,不同时间点的搜索热度;通常被用来比较相似关键字及相同关键字在不同国家的表现。

原本Google Trends有自己官方的API,不过很不幸的是已经broken了。后来有人做了非官方的版本,Python和nodejs都有,今天我们就来介绍一下nodejs版本的google-trends-api

由于受CORS的限制,当前库仅能在nodejs中使用。


用法:

        const googleTrends = require('google-trends-api');

        googleTrends.apiMethod(optionsObject, [callback])

参数:

    optionsObject: 包括以下属性的对象

  • keyword  string/array 搜索关键字
  • startTime date  查询趋势的开始日期,如果没提供,默认为2004年1月1号(谷歌趋势开始有数据的时间)
  • endTime date  查询趋势的结束日期, 默认当前时间
  • geo string/array  要查询的关键字生效的地区
  • hl  string  关键字语言, 默认英文
  • category number 关键字分类  默认所有分类
  • property string 谷歌搜索类型,默认网页搜索。
  • resolution string 关键字生效地区的粒度,例如:国家,地区,城市等
  • granularTimeResolution Boolean 是否要提供更细的时间粒度(如果开始时间和结束时间之间少于一天,此项应该填true)        

    callback:可选的回调函数,其中第一个参数是错误,第二个参数是结果。 如果没有提供回调,则返回一个Promise。


安装:

npm install google-trends-api

 调用:

const googleTrends = require('google-trends-api');

API

Promises

默认情况下,所有API都会返回一个Promise. 例如:

googleTrends.interestOverTime({keyword: 'Women\'s march'})
.then(function(results){
  console.log('These results are awesome', results);
})
.catch(function(err){
  console.error('Oh no there was an error', err);
});

回调方法

所有的API都可以通过第二个参数执行回调方法,例如:

googleTrends.interestOverTime({keyword: 'Women\'s march'}, function(err, results){
  if(err) console.error('there was an error!', err);
  else console.log('my sweet sweet results', results);
})

代理服务器

通过在query参数中指定agent来设置一个代理。以下是使用 https-proxy-agent 的情况:

const HttpsProxyAgent = require('https-proxy-agent');

let proxyAgent =  new HttpsProxyAgent('http://proxy-host:8888/');

let query = {
    keyword: 'Women\'s march',
    agent: proxyAgent
};

googleTrends.interestOverTime(query)
.then(function(results){
  console.log('These proxied results are incredible', results);
})
.catch(function(err){
  console.error('Oh no there was an error, double check your proxy settings', err);
});

多关键字

如果你想比较多个关键字,请把关键字置为数组即可,例如:

googleTrends.interestOverTime({keyword: ['Women\'s march', 'Trump Inauguration']})
.then(function(results){
  console.log('These results are awesome', results);
})
.catch(function(err){
  console.error('Oh no there was an error', err);
});

示例

在模块的根目录中,每个 API 方法都有可用的示例。注意:examples.js 中的每个示例都需要取消注释。

API 方法

当前API支持以下方法:

autoComplete

  • Google 趋势 UI 中“添加搜索词”输入框弹出的结果

Syntax

googleTrends.autoComplete({keyword: string}, cbFunc)

第一个参数包括以下部分:

  • keyword - required - type string - 关键词
  • hl - optional - type string - 返回结果的预言(默认英语)

可选的回调函数作为第二个参数(否则返回一个promise)

Example

返回'Back to School'的自动填充结果

Input
googleTrends.autoComplete({keyword: 'Back to School'})
.then(function(results) {
  console.log(results);
})
.catch(function(err) {
  console.error(err);
})
Output
{"default":{"topics":[{"mid":"/m/0414j6","title":"Back to School","type":"1986 film"},{"mid":"/m/068pw8","title":"Back to school","type":"Topic"},{"mid":"/m/04vwgn","title":"Fight Back to School","type":"1991 film"},{"mid":"/m/05357_","title":"Tax holiday","type":"Holiday"},{"mid":"/m/02pb6kt","title":"Fight Back to School II","type":"1992 film"}]}}

dailyTrends

每日搜索趋势突出显示过去 24 小时内所有搜索中流量显着增加的搜索,并每小时更新一次。这些搜索趋势显示了搜索的特定查询,以及搜索的绝对数量。返回 20 个每日热门搜索结果

Syntax

googleTrends.dailyTrends({ geo: string }, cbFunc)

第一个参数包括以下部分:

  • geo - required - type string - 国家的地理编码。例如,'US' 代表美国,'FR'代表法国.

  • hl - optional - type string - 结果的首选语言代码(默认为英语).
  • timezone - optional - type number - 首选时区(默认为时区差异,以分钟为单位,从 UTC 到当前区域设置(主机系统设置))
  • trendDate - optional - type Date object - 检索趋势信息时的日期(默认为当前日期)。请注意,查询过去 15 天以上的日期会导致错误。

可选的回调函数作为第二个参数(否则返回一个promise)

Example

返回美国地区的实时趋势故事.

Input
googleTrends.dailyTrends({
  trendDate: new Date('2019-01-10'),
  geo: 'US',
}, function(err, results) {
  if (err) {
    console.log(err);
  }else{
    console.log(results);
  }
});
Output
{
  default : [Object]{
    trendingSearchesDays : [Array]
      [0] : [Object]{
        date : String
        formattedDate: String
        trendingSearches : [Array]{
          [0] : [Object] //First trending result
        }
      [1] : [Object]{
        date : String
        formattedDate: String
        trendingSearches : [Array]{
          [0] : [Object] //first trending result
          ...
          [19] : [Object] //20th trending result
        }
      }
    }
    endDateForNextRequest : String,
    rssFeedPageUrl : String,
  }
}

interestOverTime

相对于给定区域和时间的图表最高点的搜索热度(100 是该词的最高流行度)

Syntax

googleTrends.interestOverTime({keyword: string, startTime: Date, endTime: Date, geo: string}, cbFunc)

第一个参数是一个对象,具有以下key值:

  • keyword - required - type string or array
  • startTime - optional - type Date object 
  • endTime - optional - type Date object
  • geo - optional - type string or array
  • hl - optional - type string
  • timezone - optional - type number
  • category - optional - type number
  • granularTimeResolution - optional - type boolean
Example 1

返回目前为止“情人节”的搜索兴趣(默认从 2004-01-01 到今天))

Input
googleTrends.interestOverTime({keyword: 'Valentines Day'})
.then(function(results){
  console.log(results);
})
.catch(function(err){
  console.error(err);
});
Output
{"default":{"timelineData":[{"time":"1072915200","formattedTime":"Jan 2004","formattedAxisTime":"Jan 1, 2004","value":[26],"formattedValue":["26"]},{"time":"1075593600","formattedTime":"Feb 2004","formattedAxisTime":"Feb 1, 2004","value":[74],"formattedValue":["74"]},
...
{"time":"1483228800","formattedTime":"Jan 2017","formattedAxisTime":"Jan 1, 2017","value":[18],"formattedValue":["18"]},{"time":"1485907200","formattedTime":"Feb 2017","formattedAxisTime":"Feb 1, 2017","value":[72],"formattedValue":["72"]}],"averages":[]}}

interestByRegion

查看您的关键字在指定时间范围内的哪个位置最受欢迎。值的计算范围为 0 到 100,其中 100 是最受欢迎的位置,占该位置总搜索量的一部分。

Syntax

googleTrends.interestByRegion({keyword: string, startTime: Date, endTime: Date, geo: string, resolution: string}, cbFunc)

参数和上面一样,不变(以后的方法不再重复描述)

Example 1

返回 2017 年 2 月 1 日至 2017 年 2 月 6 日世界各地城市对“Donald Trump”的关注度。

Input
googleTrends.interestByRegion({keyword: 'Donald Trump', startTime: new Date('2017-02-01'), endTime: new Date('2017-02-06'), resolution: 'CITY'})
.then((res) => {
  console.log(res);
})
.catch((err) => {
  console.log(err);
})
Output
{"default":{"geoMapData":[{"coordinates":{"lat":18.594395,"lng":-72.3074326},"geoName":"Port-au-Prince","value":[100],"formattedValue":["100"],"maxValueIndex":0},{"coordinates":{"lat":43.467517,"lng":-79.6876659},"geoName":"Oakville","value":[90],"formattedValue":["90"],"maxValueIndex":0},
...
{"coordinates":{"lat":40.9312099,"lng":-73.8987469},"geoName":"Yonkers","value":[69],"formattedValue":["69"],"maxValueIndex":0}]}}

realtimeTrends

实时搜索趋势突出显示过去 24 小时内 Google 平台上的热门报道,并实时更新。返回 13 个实时热门故事

Syntax

googleTrends.realTimeTrends({ geo: string }, cbFunc)

参数:略

Example

返回美国地区的实时趋势故事.

Input
googleTrends.realTimeTrends({
    geo: 'US',
    category: 'all',
}, function(err, results) {
    if (err) {
       console.log(err);
    } else {
      console.log(results);
    } 
});
Output
{
	featuredStoryIds : [Array], // Empty
	trendingStoryIds : [Array], // 300 trending story IDs
  storySummaries : [Object]
    {
	  featuredStories : [Array], // Empty
    trendingStories : [Array], // 13 top trending stories
    },
	date : "Date-String",
  hideAllImages : Boolean,
}

relatedQueries

搜索您的字词的用户也搜索了这些查询

Syntax

googleTrends.relatedQueries({keyword: string, startTime: Date, endTime: Date, geo: string}, cbFunc)

参数:略

Example

使用默认开始时间、结束时间和地理类别返回与“Westminster Dog show”相关的热门查询

Input
googleTrends.relatedQueries({keyword: 'Westminster Dog Show'})
.then((res) => {
  console.log(res);
})
.catch((err) => {
  console.log(err);
})
Output
{"default":{"rankedList":[{"rankedKeyword":[{"query":"dog show 2016","value":100,"formattedValue":"100","link":"/"},{"query":"2016 westminster dog show","value":95,"formattedValue":"95","link":"/"},
...
{"query":"dogs","value":20,"formattedValue":"20","link":"/"}]},{"rankedKeyword":[{"query":"dog show 2016","value":836500,"formattedValue":"Breakout","link":"/"},{"query":"2016 westminster dog show","value":811550,"formattedValue":"Breakout","link":"/"},
...
{"query":"who won the westminster dog show","value":59000,"formattedValue":"Breakout","link":"/"}]}]}}



relatedTopics

搜索您的字词的用户也搜索了这些主题

Syntax

googleTrends.relatedTopics({keyword: string, startTime: Date, endTime: Date, geo: string}, cbFunc)

参数:略

Example

返回 2015 年 1 月 1 日至 2017 年 2 月 10 日与“Chipotle”相关的热门话题.

Input
googleTrends.relatedTopics({keyword: 'Chipotle', startTime: new Date('2015-01-01'), endTime: new Date('2017-02-10')})
.then((res) => {
  console.log(res);
})
.catch((err) => {
  console.log(err);
})
Output
{"default":{"rankedList":[{"rankedKeyword":[{"topic":{"mid":"/m/01b566","title":"Chipotle Mexican Grill","type":"Restaurant company"},"value":100,"formattedValue":"100","link":"/"},{"topic":{"mid":"/m/02f217","title":"Chipotle","type":"Jalape\u00f1o"},"value":5,"formattedValue":"5","link":"/"},
...
{"topic":{"mid":"/m/01xg7s","title":"Chorizo","type":"Topic"},"value":0,"formattedValue":"0","link":"/"}]},{"rankedKeyword":[{"topic":{"mid":"/m/09_yl","title":"E. coli","type":"Bacteria"},"value":40700,"formattedValue":"Breakout","link":"/"},
...
{"topic":{"mid":"/m/0dqc4","title":"Caridea","type":"Animal"},"value":40,"formattedValue":"+40%","link":"/"}]}]}}


文档就介绍到这里,想看原文的点击以下链接

github地址:https://github.com/pat310/google-trends-api

npm库:https://www.npmjs.com/package/google-trends-api



发表评论

0 评论