Google Trends Hacking

給我資料

最近試著在蒐集一些看起來有點用途的資料,Google Trends 看起來是一個 “滿有價值” 的資料,是 Google 蒐集的全球趨勢資料,包含最近流行的關鍵字,關鍵字與關鍵字的比較,etc.,但 Google 並沒有提供相關的 API 介面,所以要取得資料的做法我第一個想到的是 Crawler,於是順手找了一下大神的開源專案,左看右看上看下看,都沒認真看,都覺得不順手XD。

Hack Fun

剛好讀到一篇很有趣的文章-hacking the google trends api,文章內容提到了幾個有趣的 end-point,像這樣

http://www.google.com/trends/fetchComponent?hl=en-US&q=html5,jquery&cid=TIMESERIES_GRAPH_0&export=5&w=500&h=300

URI http://www.google.com/trends/fetchComponent
hl en-US
q keywords
cid TIMESERIES_GRAPH_0/RISING_QUERIES_1_0/business_and_politics
export 5
w width
h height

還有這樣 curl 大絕招

curl --data "ajax=1&htd=20131111&pn=p1&htv=l" http://www.google.com/trends/hottrends/hotItems

還有直接把這個貼在 Browser

http://hawttrends.appspot.com/api/terms/

這邊還有大神整理的參數對照表,實在棒棒der。
http://myweb.fcu.edu.tw/~mhsung/Research/InformationSystem/JSON/JSON_24.htm

Browser Dev-Tools

看完大神的示範後,發現其實有些開放的資料並不一定要特別透過 Crawler 去取得,太沒效率了。而且大部份的瀏覽器都有提供開發工具,Hacking 那些 end-point 其實滿簡單的,比如說 Chrome 的開發者工具,我以這個潮潮的頁面來做示範好了

https://www.google.com/trends/hottrends/visualize?pn=p1

image-4

就可以愉快得到一串 curl

curl 'https://www.google.com/trends/hottrends/visualize/internal/data' -H 'Referer: https://www.google.com/trends/hottrends/visualize?pn=p1' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36' --compressed

就可以愉快地寫 api 了

Sample

Reference