通过Jsoup获得top100下载地址后台下载mp3

  通过解析top100的页面获得歌曲的下载地址,后台下载歌曲。比较粗糙,有时会报

 

  java.net.SocketTimeoutException: Read timed out

 

  找了很久,不知道什么原因。需要jsoup支持

 

  jsoup下载地址:

 

  http://jsoup.org/download

 

  代码如下:

 

 

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


public class GetTop100 {
	public static void main(String[] args) throws Exception{
		String keyword = "B小调雨后";
		boolean isDownload = true;
		String encodeKeyword = URLEncoder.encode(keyword, "UTF-8");
		String searchUrl = "http://www.top100.cn/search/?act=allsong&keyword=" + encodeKeyword;
		String downloadpageurl = getDownloadPageFromURL(searchUrl);
		if (isEmpty(downloadpageurl)) {
			System.out.println("没有找到" + keyword + "!");
			System.exit(0);
		} else {
			System.out.println("找到了" + keyword);
		}
		if (!isDownload) return;
		String downloadUrl = getDownloadUrl(downloadpageurl);
		System.out.println(downloadUrl);
		String filename = "C:\\" + keyword + ".mp3";
		downLoadMp3FromUrl(downloadUrl, filename);
		System.out.println("完成");
	}
	
	static String getDownloadPageFromURL(String url) throws Exception {
		System.out.println(url);
		//String url = "http://www.top100.cn/search/?act=allsong&keyword=%E5%A4%9C%E5%A4%9C%E5%A4%9C%E5%A4%9C";
		Document document = Jsoup.connect(url).get();
		Element searchTopDiv = document.getElementsByClass("listen_search_top").get(0);
		Elements resultDivs = searchTopDiv.getElementsByTag("div");
		if (!resultDivs.isEmpty()) {
			String msg = resultDivs.get(0).text();
			if (msg.contains("没有找到")) {
				return null;
			}
		}
		if (!searchTopDiv.text().contains("含有")) {
			return null;
		}
		Elements no5s = document.getElementsByClass("No5");
		Element no5 = no5s.first();
		Element down = no5.getElementsByClass("down").first();
		String href = down.attr("href");
		String productid = href.substring(href.indexOf("'") + 2, href.lastIndexOf("'"));
		String downloadpageurl = "http://www.top100.cn/download/download.php?Productid=" + productid ;
		return downloadpageurl;
	}
	static String getDownloadUrl(String url) throws Exception{
		Document document = Jsoup.connect(url).get();
		Elements downloadDiv = document.getElementsByClass("Listen_downloadtopcon");
		Element no6 = downloadDiv.get(0).getElementsByClass("No6").get(0);
		Element a = no6.getElementsByTag("a").get(0);
		String href = a.attr("href");
		String downloadUrl = "http://www.top100.cn" + href;
		return downloadUrl;
	}
	static void downLoadMp3FromUrl(String downloadUrl, String filename) throws Exception{
		URL download = new URL(downloadUrl);
		URLConnection con = download.openConnection();
		InputStream is = con.getInputStream();
		byte[] bs = new byte[1024];
		int len;
		OutputStream os = new FileOutputStream(filename);
		while((len = is.read(bs)) != -1) {
			os.write(bs, 0, len);
		}
		os.close();
		is.close();
	}
	static boolean isEmpty(Object object) {
		if (object == null) {
			return true;
		} else if (object instanceof String) {
			if (object == null || object.toString().length() == 0) {
				return true;
			}
		}
		return false;
	}
}
 

 

 

你可能感兴趣的:(通过Jsoup获得top100下载地址后台下载mp3)