2011-07-11 109 views
1

我是一名编程新手,我有一个项目,我必须编写一个Ruby脚本以从github上检索指定存储库上的信息,解析JSON格式的数据,并以可用格式打印命令行。使用ruby进行网页抓取

我已经签出机械指南。我可以检查以完成此任何文件?

+0

我只安装了hpricot和mechanize,没有安装其他人的权限。 – kamenraider2

回答

3

使用Github的Repositories API。你想要的一切都是在那里完成的,没有刮或怪异的黑客。 JSON格式的响应默认情况下。

+0

谢谢。我以前也看到过,但事情是我没有卷曲。任何建议? – kamenraider2

+0

按照@jdc,RESTClient建议的方式使用httparty,或者仅使用普通的旧的Net:HTTP。 –

1

继@Douglas的回应。

require 'httparty' 
class Repository 
    include HTTParty 
    base_uri 'www.github.com' 
end 
response = Repository.get('/api/v2/json/repos/show/joncooper/beanstalkd') 

require 'awesome_print' 
>> ap response.parsed_response 
{ 
    "repository" => { 
       "name" => "beanstalkd", 
       "size" => 128, 
      "created_at" => "2011/04/29 09:43:43 -0700", 
      "has_wiki" => true, 
       "parent" => "kr/beanstalkd", 
       "private" => false, 
      "watchers" => 1, 
       "fork" => true, 
      "language" => "C", 
        "url" => "https://github.com/joncooper/beanstalkd", 
      "pushed_at" => "2011/07/05 22:10:53 -0700", 
      "open_issues" => 0, 
     "has_downloads" => true, 
      "has_issues" => false, 
      "homepage" => "http://kr.github.com/beanstalkd/", 
       "forks" => 0, 
      "description" => "Beanstalk is a simple, fast work queue.", 
       "source" => "kr/beanstalkd", 
       "owner" => "joncooper" 
    } 
} 

更多见http://httparty.rubyforge.org/:你想要做什么用GitHub的API和HTTParty宝石是很容易。