gpt4 book ai didi

google-code - 如何按星级对 Google Code 项目进行排序/过滤?

转载 作者:行者123 更新时间:2023-12-04 05:41:14 25 4
gpt4 key购买 nike

我想知道在 Google Code 上托管的哪些项目拥有最多的星星,尤其是在按标签搜索时,如下所示:

https://code.google.com/hosting/search?q=label%3aAndroid

最佳答案

项目搜索页面不支持按星级排序。能够编写一些页面抓取代码来获取所需的信息。

希望能帮助到你。

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;
}

关于google-code - 如何按星级对 Google Code 项目进行排序/过滤?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16830406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com