目的:
为了比较几个关键词哪一个更hot 一些,我通常在google上搜索,比较返回的结果数。
当我想在N多Wiki系统中选一个研究一下的时候,我发现几乎有上百种wiki系统,总不能让我一个个输到Google,然后一个个copy结果统计吧。。
于是我想产生一种永久解决方案,做一个Google PK,用于比较结果数(当然我在网上找过有没有现成的东西,Google Trends 限制关键词5个,还有网站可以PK两个关键词,还有可以Google 和Baidu比较的,都不满足我的需求)
效果:
具体效果可以看
http://sites.google.com/site/ningssite/
或:
http://time-stamp.appspot.com/s/GooglePK.html
实现:
最原始的方法是用解析Google 返回的页面,从里面得出结果数,不过这太野蛮,为了找个文明点的办法,开始找Google API
先找到的是Google Ajax Search API ,这是一个搜索控件:
这是一个js控件,呈现在Web页面上的。
我希望的不是一个这样的控件,而是一个API接口,可以从中得到返回的数据,用fireBug观察Ajax Search API 返回的结果,
得到如下数据,
- a = {
- "responseData": {
- "results": [{
- "GsearchResultClass": "GwebSearch",
- "unescapedUrl": "http://www.albertaviews.ab.ca/",
- "url": "http://www.albertaviews.ab.ca/",
- "visibleUrl": "www.albertaviews.ab.ca",
- "cacheUrl": "http://www.google.com/search?q/u003dcache:4tWNzgZymtoJ:www.albertaviews.ab.ca",
- "title": "/u003cb/u003eAlberta/u003c/b/u003e Views - Perspectives On A Province",
- "titleNoFormatting": "Alberta Views - Perspectives On A Province",
- "content": "Explores the political, social and cultural life of /u003cb/u003eAlberta/u003c/b/u003e from a progressive perspective."
- }, {
- "GsearchResultClass": "GwebSearch",
- "unescapedUrl": "http://www.concordia.ab.ca/",
- "url": "http://www.concordia.ab.ca/",
- "visibleUrl": "www.concordia.ab.ca",
- "cacheUrl": "http://www.google.com/search?q/u003dcache:lm7ZimyY-FQJ:www.concordia.ab.ca",
- "title": "Concordia University College of /u003cb/u003eAlberta/u003c/b/u003e",
- "titleNoFormatting": "Concordia University College of Alberta",
- "content": "A University Degree-granting Christian liberal arts educational institution operated by Lutheran Church-Canada: Concordia University College of /u003cb/u003eAlberta/u003c/b/u003e."
- },
- ....
- "cursor": {
- "pages": [{
- "start": "0",
- "label": 1
- }, {
- "start": "4",
- "label": 2
- }, {
- "start": "8",
- "label": 3
- }, {
- "start": "12",
- "label": 4
- }, {
- "start": "16",
- "label": 5
- }, {
- "start": "20",
- "label": 6
- }, {
- "start": "24",
- "label": 7
- }, {
- "start": "28",
- "label": 8
- }],
- "estimatedResultCount": "1610000",
- "currentPageIndex": 0,
- "moreResultsUrl": "http://www.google.com/search?oe/u003dutf8/u0026ie/u003dutf8/u0026source/u003duds/u0026start/u003d0/u0026hl/u003dzh-CN/u0026q/u003dParis+Hilton"
- }
- },
- "responseDetails": null,
- "responseStatus": 200
- }
其中“estimatedResultCount”参数正是我需要的
搜estimatedResultCount,发现有两个API接口提供这个参数:
Google SOAP search API
Google JSON search API (Google REST Search API)
不过SOAP 似乎不太流行( Google half-way cancelled their SOAP API a while ago)
(Dived into Python里面有讲写SOAP 的Google Search应用)
我选择了JSON API,可以做成一个HTML文档,方便发布~
仔细读了http://code.google.com/apis/ajaxsearch/documentation/
主要是其中Custom Search Form一节,把代码拷下来,稍微修改就有了一个得到返回结果数的页面,
关于呈现,我使用了Google Visualization API 中的表格http://code.google.com/apis/visualization/documentation/gallery/table.html
使用很简单。效果很强大~
为了发布Google PK,我又把它做成了一个Gadget,主要参考http://code.google.com/apis/visualization/documentation/dev/gadgets.html
把一个HTML页面改造成Gadget很简单,这有个简单的原则,
FROM : http://code.google.com/apis/gadgets/docs/fundamentals.html
做Gadget可以用Google提供的一个在线编辑器 http://code.google.com/apis/gadgets/docs/tools.html(感慨Google的强大)
源代码:
Code
发布的Gadget代码:http://hosting.gmodules.com/ig/gadgets/file/104484621072936284877/GooglePK.xml
2万+



被折叠的 条评论
为什么被折叠?



