【发布时间】:2015-07-28 21:28:02
【问题描述】:
我正在尝试检索可供下载的 .zip 文件的名称。参考 jSoup "cookbook",我发现该示例使用 "%s (%s)",它返回 hyperlink (download name)。我只想要后一部分,但很难区分两者。到目前为止,这是我所拥有的:
public static void getNames() throws IOException{
String url = "http://download.cyanogenmod.org/?device=d855";
print("Fetching %s...", url);
Document doc = Jsoup.connect(url).get();
Elements links = doc.select("a");
downloadNames = new ArrayList<>();
// print("\nLinks: (%d)", links.size());
for (Element link : links) {
downloadNames.add(print("<%s> (%s)", link.attr("abs:href"), trim(link.text(), 35)));
}
int x = 0;
while (x < downloadNames.size()) {
// System.out.println(downloadNames.get(x));
x++;
}
uniqueDownloadNames = new ArrayList<>();
int y = 0;
while (y < downloadNames.size()) {
if (downloadNames.get(y).contains(".zip") && downloadNames.get(y).startsWith("<http://download")) {
uniqueDownloadNames.add(downloadNames.get(y));
}
y++;
}
int z = 0;
while (z < uniqueDownloadNames.size()) {
System.out.println(uniqueDownloadNames.get(z));
z++;
}
}
private static String print(String msg, Object... args) {
// System.out.println(String.format(msg, args));
String s = String.format(msg, args);
return s;
}
private static String trim(String s, int width) {
if (s.length() > width)
return s.substring(0, width-1) + ".";
else
return s;
}
【问题讨论】: