那天朋友发我一个txt文件,里面是他喜欢的歌曲名称,让我帮忙写个程序把上面歌曲的下载地址找来。 我想到了HtmlUnit, 于是YY几分钟后写了个hello world. 如下
-
public static void main(String[] args)
-
throws FailingHttpStatusCodeException, MalformedURLException,
-
IOException {
-
-
File writeFile = new File("/Users/jferson/Desktop/rs.txt");
-
if (writeFile.isFile())
-
writeFile.mkdirs();
-
-
List<String> songInfos = FileUtils.readLines(new File(
-
"/Users/jferson/Desktop/500.txt"));
-
-
String mp3UrlPrefix = "";
-
String mp3UrlSuffix = "&lm=-1&f=ms&tn=baidump3&ct=134217728&lf=&rn=";
-
if (songInfos != null && songInfos.size() > 0) {
-
String song = null;
-
String singer = null;
-
StringBuffer sb = new StringBuffer();
-
for (String songInfo : songInfos) {
-
String[] splits = songInfo.split("_");
-
if (splits != null && splits.length == 2) {
-
song = splits[0];
-
singer = splits[1];
-
-
String ma3UrlPath = mp3UrlPrefix
-
+ song.replaceAll(" ", "+") + mp3UrlSuffix;
-
-
WebClient webClient = new WebClient();
-
-
webClient.setThrowExceptionOnScriptError(false);
-
-
webClient.setThrowExceptionOnFailingStatusCode(false);
-
-
HtmlPage gamePage = webClient.getPage(ma3UrlPath);
-
-
if (gamePage == null
-
|| gamePage.getElementById("songResults") == null)
-
continue;
-
-
DomNodeList<HtmlElement> trElements = gamePage
-
.getElementById("songResults")
-
.getElementsByTagName("tr");
-
int i = 1;
-
sb.append("歌曲:").append(song).append("rn");
-
for (HtmlElement htmlElement : trElements) {
-
DomNodeList<HtmlElement> tdElements = htmlElement
-
.getElementsByTagName("td");
-
if (tdElements == null || tdElements.size() == 0)
-
continue;
-
boolean bingo = false;
-
if (i > 2) {
-
break;
-
}
-
HtmlElement thridElement = tdElements.get(2);
-
if (thridElement.getAttribute("class")
-
.equalsIgnoreCase("third")) {
-
-
if (thridElement
-
.asText()
-
.toLowerCase()
-
.trim()
-
.equalsIgnoreCase(
-
singer.toLowerCase().trim())) {
-
-
bingo = true;
-
} else {
-
continue;
-
}
-
}
-
HtmlElement downElement = tdElements.get(6);
-
if (downElement.getAttribute("class").equalsIgnoreCase(
-
"down")
-
&& bingo) {
-
HtmlPage p = downElement.getElementsByTagName("a")
-
.get(0).click();
-
String pageHtml = p.getWebResponse()
-
.getContentAsString();
-
if (pageHtml.lastIndexOf("/j?j=2&url=") != -1) {
-
String downloadPath = null;
-
pageHtml = StringUtils
-
.substring(
-
pageHtml,
-
pageHtml.lastIndexOf("/j?j=2&url=") + 11,
-
pageHtml.indexOf("return startDownLoad()") - 11);
-
downloadPath = pageHtml.replaceAll("%2F", "/")
-
.replaceAll("%3A", ":")
-
.replaceAll("%3F", "?")
-
.replaceAll("%3D", "=");
-
-
sb.append("下载地址" + i + ": ")
-
.append(downloadPath).append("rn");
-
i++;
-
}
-
-
}
-
}
-
if (i == 1) {
-
sb.append("找不到匹配的下载地址!rn");
-
}
-
}
-
}
-
FileUtils.writeStringToFile(writeFile, sb.toString());
-
}
-
-
}
阅读(1187) | 评论(0) | 转发(0) |