Chinaunix首页 | 论坛 | 博客
  • 博客访问: 188357
  • 博文数量: 60
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 385
  • 用 户 组: 普通用户
  • 注册时间: 2013-02-19 21:43
个人简介

readonly

文章分类

全部博文(60)

文章存档

2013年(60)

我的朋友

分类: Java

2013-03-01 10:54:46

     那天朋友发我一个txt文件,里面是他喜欢的歌曲名称,让我帮忙写个程序把上面歌曲的下载地址找来。 我想到了HtmlUnit, 于是YY几分钟后写了个hello world. 如下

点击(此处)折叠或打开

  1. public static void main(String[] args)
  2.             throws FailingHttpStatusCodeException, MalformedURLException,
  3.             IOException {

  4.         File writeFile = new File("/Users/jferson/Desktop/rs.txt");
  5.         if (writeFile.isFile())
  6.             writeFile.mkdirs();

  7.         List<String> songInfos = FileUtils.readLines(new File(
  8.                 "/Users/jferson/Desktop/500.txt"));

  9.         String mp3UrlPrefix = "";
  10.         String mp3UrlSuffix = "&lm=-1&f=ms&tn=baidump3&ct=134217728&lf=&rn=";
  11.         if (songInfos != null && songInfos.size() > 0) {
  12.             String song = null;
  13.             String singer = null;
  14.             StringBuffer sb = new StringBuffer();
  15.             for (String songInfo : songInfos) {
  16.                 String[] splits = songInfo.split("_");
  17.                 if (splits != null && splits.length == 2) {
  18.                     song = splits[0];
  19.                     singer = splits[1];

  20.                     String ma3UrlPath = mp3UrlPrefix
  21.                             + song.replaceAll(" ", "+") + mp3UrlSuffix;

  22.                     WebClient webClient = new WebClient();

  23.                     webClient.setThrowExceptionOnScriptError(false);

  24.                     webClient.setThrowExceptionOnFailingStatusCode(false);

  25.                     HtmlPage gamePage = webClient.getPage(ma3UrlPath);

  26.                     if (gamePage == null
  27.                             || gamePage.getElementById("songResults") == null)
  28.                         continue;

  29.                     DomNodeList<HtmlElement> trElements = gamePage
  30.                             .getElementById("songResults")
  31.                             .getElementsByTagName("tr");
  32.                     int i = 1;
  33.                     sb.append("歌曲:").append(song).append("rn");
  34.                     for (HtmlElement htmlElement : trElements) {
  35.                         DomNodeList<HtmlElement> tdElements = htmlElement
  36.                                 .getElementsByTagName("td");
  37.                         if (tdElements == null || tdElements.size() == 0)
  38.                             continue;
  39.                         boolean bingo = false;
  40.                         if (i > 2) {
  41.                             break;
  42.                         }
  43.                         HtmlElement thridElement = tdElements.get(2);
  44.                         if (thridElement.getAttribute("class")
  45.                                 .equalsIgnoreCase("third")) {

  46.                             if (thridElement
  47.                                     .asText()
  48.                                     .toLowerCase()
  49.                                     .trim()
  50.                                     .equalsIgnoreCase(
  51.                                             singer.toLowerCase().trim())) {

  52.                                 bingo = true;
  53.                             } else {
  54.                                 continue;
  55.                             }
  56.                         }
  57.                         HtmlElement downElement = tdElements.get(6);
  58.                         if (downElement.getAttribute("class").equalsIgnoreCase(
  59.                                 "down")
  60.                                 && bingo) {
  61.                             HtmlPage p = downElement.getElementsByTagName("a")
  62.                                     .get(0).click();
  63.                             String pageHtml = p.getWebResponse()
  64.                                     .getContentAsString();
  65.                             if (pageHtml.lastIndexOf("/j?j=2&url=") != -1) {
  66.                                 String downloadPath = null;
  67.                                 pageHtml = StringUtils
  68.                                         .substring(
  69.                                                 pageHtml,
  70.                                                 pageHtml.lastIndexOf("/j?j=2&url=") + 11,
  71.                                                 pageHtml.indexOf("return startDownLoad()") - 11);
  72.                                 downloadPath = pageHtml.replaceAll("%2F", "/")
  73.                                         .replaceAll("%3A", ":")
  74.                                         .replaceAll("%3F", "?")
  75.                                         .replaceAll("%3D", "=");

  76.                                 sb.append("下载地址" + i + ": ")
  77.                                         .append(downloadPath).append("rn");
  78.                                 i++;
  79.                             }

  80.                         }
  81.                     }
  82.                     if (i == 1) {
  83.                         sb.append("找不到匹配的下载地址!rn");
  84.                     }
  85.                 }
  86.             }
  87.             FileUtils.writeStringToFile(writeFile, sb.toString());
  88.         }

  89.     }

阅读(1180) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~