2013-05-30 81 views

回答

7

項目搜索頁面不支持按星號排序。能夠寫一些頁面廢料代碼來獲取所需的信息。

希望它有幫助。

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.Comparator; 
import java.util.List; 


public class ReadGoogleProjectSortByStars { 

    public static void main(String[] args) throws IOException { 
     String urlPath = "https://code.google.com/hosting/search?q=label%3AAndroid&filter=0&mode=&start="; 
     // urlPath = "https://code.google.com/hosting/search?q=label%3AAndroid+stackoverflow&projectsearch=Search+projects&filter=0&mode=&start="; 
     int start = 0; 
     List<Project> projects = new ArrayList<Project>(); 
     boolean done = false; 
     while(!done) { 
      String urlStr = urlPath + start; 
      URL url = new URL(urlStr); 
      BufferedReader in = new BufferedReader(
      new InputStreamReader(url.openStream())); 

      String inputLine; 
      String projectUrl = null, stars = null; 
      while ((inputLine = in.readLine()) != null) { 
       int urlIndex = -1, starIndex = -1; 
       if(inputLine.contains(" style=\"font-size:medium\">") && (urlIndex = inputLine.indexOf(" href=\"/p/")) != -1) { 
        if(projectUrl != null) { 
         Project project = new Project(); 
         project.url = projectUrl; 
         project.stars = "0"; 
         projects.add(project); 
        } 
        String projectTempUrl = inputLine.substring(urlIndex + " href=\"/p/".length()); 
        projectUrl = "https://code.google.com/p/" + projectTempUrl.substring(0, projectTempUrl.indexOf("\"")); 
       } 
       if((starIndex = inputLine.indexOf("id=\"star_count-")) != -1) { 
        stars = inputLine.substring(inputLine.indexOf(">") + 1, inputLine.indexOf("</span>")); 
        Project project = new Project(); 
        project.url = projectUrl; 
        project.stars = stars; 
        projects.add(project); 
        projectUrl = stars = null; 
       } 
       if(inputLine.contains(" - did not generate any results.")) { 
        done = true; 
        break; 
       } 
      } 
      in.close(); 
      start +=10; 
      if(projectUrl != null) { 
       Project project = new Project(); 
       project.url = projectUrl; 
       project.stars = "0"; 
       projects.add(project); 
      } 
     } 
     Collections.sort(projects, new Comparator<Project>() { 

      @Override 
      public int compare(Project project1, Project project2) { 
       Integer stars1 = Integer.parseInt(project1.stars); 
       Integer stars2 = Integer.parseInt(project2.stars); 
       return -stars1.compareTo(stars2); 
      } 

     }); 
     System.out.println("Total projects:" +projects.size()); 
     for (Project project : projects) { 
      System.out.println(project.url + ":" + project.stars); 
     } 
    } 
} 

class Project { 
    String url; 
    String stars; 
} 
+0

不錯的想法,但不能在這裏測試,因爲我在代理環境(代理支持會很好) – MRalwasser

+0

請參閱http://www.java.com/en/download/help/proxy_setup.xml和http:// stackoverflow.com/a/120802/276263 – krishnakumarp

+0

謝謝,代理問題已解決 - 但結果缺少許多項目。例如。如果您使用http://code.google.com/hosting/search?q=label%3AJava&filter=0&mode=&start= as urlpath,則缺少番石榴項目,該項目有超過4300顆星有趣的 – MRalwasser

0

我會說使用&sort=stars因爲他們在支持做谷歌代碼,但它不能很好地工作。我不確定這是不幸的...

相關問題