原来已经有人写过了:
http://github.com/daimpi/google-rest/tree/master
引用
DESCRIPTION:
A Simple Ruby wrapper for the Google REST API
FEATURES/PROBLEMS:
No Problems.. my programs are perfect[size=large][/size]
哇咔咔..
签下来试用一下,还真不错:
$:.unshift File.join(File.dirname(__FILE__), "..", "lib")
require'pp'
require'google-rest'
include GoogleRest
client = GoogleSearch.new
#:type可设置 "web","books","news","blogs","video","local","images","patent"等
#分别对应搜网页,搜书,新闻...
#hl可选有"en","zh-cn"等..
results = client.search :q => "ruby", :hl =>'zh-cn'
pp results
results.each do |result|
puts "+"*30
puts ":cache url: #{result.cache_url}"
puts ":content: #{result.content}"
puts ":gsearch_result_class: #{result.g_search_result_class}"
puts ":title #{result.title}"
puts ":title no formatting: #{result.title_no_formatting}"
puts ":url: #{result.url}"
puts ":unescaped url: #{result.unescaped_url}"
puts ":visible url: #{result.visible_url}"
end
[#<GoogleResult:0x2da0bcc
@cache_url=
"http://www.google.com/search?q=cache:U0idxbaGKSwJ:www.ruby-lang.org",
@content=
"A dynamic, interpreted, open source programming language with a focus on simplicity and productivity. Site includes news, downloads, documentation, <b>...</b>",
@gsearch_result_class="GwebSearch",
@title="<b>Ruby</b> Programming Language",
@title_no_formatting="Ruby Programming Language",
@unescaped_url="http://www.ruby-lang.org/",
@url="http://www.ruby-lang.org/",
@visible_url="www.ruby-lang.org">,
#<GoogleResult:0x2da0bb8
@cache_url=
"http://www.google.com/search?q=cache:Vpzq4nfiRmwJ:www.ruby-lang.org",
@content=
"\346\234\254\347\275\221\347\253\231\345\210\266\344\275\234\345\267\245\345\205\267\344\275\277\347\224\250<b>Ruby</b> \345\222\214Radiant CMS\357\274\214\345\271\266\344\270\224\346\234\211\346\235\245\350\207\252<b>Ruby</b> \347\244\276\345\214\272\344\270\200\344\272\233\344\273\244\344\272\272\351\252\204\345\202\262\347\232\204\344\274\232\345\221\230\345\205\261\345\220\214 \347\273\264\346\212\244\343\200\202 \345\246\202\346\236\234\346\202\250\346\234\211\344\273\273\344\275\225\347\226\221\351\227\256\346\210\226\350\200\205\344\270\215\346\230\216\347\231\275\347\232\204\346\263\250\351\207\212\350\257\267\350\201\224\347\263\273\346\210\221\344\273\254\347\232\204\347\253\231\347\202\271\347\256\241\347\220\206\345\221\230\343\200\202",
@gsearch_result_class="GwebSearch",
@title=
"<b>Ruby</b> \347\250\213\345\272\217\350\256\276\350\256\241\350\257\255\350\250\200\345\256\230\346\226\271\347\275\221\347\253\231",
@title_no_formatting=
"Ruby \347\250\213\345\272\217\350\256\276\350\256\241\350\257\255\350\250\200\345\256\230\346\226\271\347\275\221\347\253\231",
@unescaped_url="http://www.ruby-lang.org/zh_CN/",
@url="http://www.ruby-lang.org/zh_CN/",
@visible_url="www.ruby-lang.org">,
#<GoogleResult:0x2d9fd1c
@cache_url=
"http://www.google.com/search?q=cache:qnANn5mkKr8J:en.wikipedia.org",
@content=
"<b>Ruby</b> is a dynamic, reflective, general purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. <b>...</b>",
@gsearch_result_class="GwebSearch",
@title=
"<b>Ruby</b> (programming language) - Wikipedia, the free encyclopedia",
@title_no_formatting=
"Ruby (programming language) - Wikipedia, the free encyclopedia",
@unescaped_url="http://en.wikipedia.org/wiki/Ruby_(programming_language)",
@url="http://en.wikipedia.org/wiki/Ruby_(programming_language)",
@visible_url="en.wikipedia.org">,
#<GoogleResult:0x2d9edf4
@cache_url=
"http://www.google.com/search?q=cache:oR7CKgTNjC4J:rubyonrails.org",
@content=
"RoR home; full stack, Web application framework optimized for sustainable programming productivity, allows writing sound code by favoring convention over <b>...</b>",
@gsearch_result_class="GwebSearch",
@title="<b>Ruby</b> on Rails",
@title_no_formatting="Ruby on Rails",
@unescaped_url="http://rubyonrails.org/",
@url="http://rubyonrails.org/",
@visible_url="rubyonrails.org">]
++++++++++++++++++++++++++++++
:cache url: http://www.google.com/search?q=cache:U0idxbaGKSwJ:www.ruby-lang.org
:content: A dynamic, interpreted, open source programming language with a focus on simplicity and productivity. Site includes news, downloads, documentation, <b>...</b>
:gsearch_result_class:
:title <b>Ruby</b> Programming Language
:title no formatting: Ruby Programming Language
:url: http://www.ruby-lang.org/
:unescaped url: http://www.ruby-lang.org/
:visible url: www.ruby-lang.org
++++++++++++++++++++++++++++++
:cache url: http://www.google.com/search?q=cache:Vpzq4nfiRmwJ:www.ruby-lang.org
:content: 本网站制作工具使用<b>Ruby</b> 和Radiant CMS,并且有来自<b>Ruby</b> 社区一些令人骄傲的会员共同 维护。 如果您有任何疑问或者不明白的注释请联系我们的站点管理员。
:gsearch_result_class:
:title <b>Ruby</b> 程序设计语言官方网站
:title no formatting: Ruby 程序设计语言官方网站
:url: http://www.ruby-lang.org/zh_CN/
:unescaped url: http://www.ruby-lang.org/zh_CN/
:visible url: www.ruby-lang.org
++++++++++++++++++++++++++++++
:cache url: http://www.google.com/search?q=cache:qnANn5mkKr8J:en.wikipedia.org
:content: <b>Ruby</b> is a dynamic, reflective, general purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. <b>...</b>
:gsearch_result_class:
:title <b>Ruby</b> (programming language) - Wikipedia, the free encyclopedia
:title no formatting: Ruby (programming language) - Wikipedia, the free encyclopedia
:url: http://en.wikipedia.org/wiki/Ruby_(programming_language)
:unescaped url: http://en.wikipedia.org/wiki/Ruby_(programming_language)
:visible url: en.wikipedia.org
++++++++++++++++++++++++++++++
:cache url: http://www.google.com/search?q=cache:oR7CKgTNjC4J:rubyonrails.org
:content: RoR home; full stack, Web application framework optimized for sustainable programming productivity, allows writing sound code by favoring convention over <b>...</b>
:gsearch_result_class:
:title <b>Ruby</b> on Rails
:title no formatting: Ruby on Rails
:url: http://rubyonrails.org/
:unescaped url: http://rubyonrails.org/
:visible url: rubyonrails.org
不过,在测试搜书的时候出现错误..查了下源代码,是这里的问题:
def initialize(result)
result.each do |key, value|
string ="#{underscore(key.to_s)}='#{value'}"
eval "@#{string}"
end
end
原来搜关键词ruby搜到了《the pragmatic programmer's guide》......看来高手也犯错误..
改了一下:
def initialize(result)
result.each do |key, value|
string = %Q{#{underscore(key.to_s)}=%q{#{value}}}
eval "@#{string}"
end
end