可以实现和点点网音乐分享功能暂时还没做界面,先留下代码
package com;
/**
* 歌曲实体
* @author hanfei
*
*/
public class Song {
private String title;
private String songUrl;
private String flashUrl;
private String image;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getFlashUrl() {
return flashUrl;
}
public void setFlashUrl(String flashUrl) {
this.flashUrl = flashUrl;
}
public void setSongUrl(String songUrl) {
this.songUrl = songUrl;
}
public String getSongUrl() {
return songUrl;
}
public void setImage(String image) {
this.image = image;
}
public String getImage() {
return image;
}
}
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
/**
* 抓取工具类
* @author hanfei
*
*/
public class SongUtil {
/**
* 根据关键词搜索歌曲
*
* @param key
* 关键词
* @return
* @throws IOException
*/
public static List seachXiaMiSongs(String key) throws IOException {
Document doc = Jsoup.connect("http://www.xiami.com/ajax/search-index")
.data("query", "Java").data("key", key)
.userAgent("Mozilla")
.cookie("auth", "token")
.timeout(6000)
.get();
String content = doc.html();
content = content.substring(content.indexOf("
- ") + 4, content
.indexOf("
"));doc = Jsoup.parse(content);
System.out.print(doc.html());
Elements links = doc.select("a[href]");
System.out.println("sssssssssss" + links.size());
List list = new ArrayList();
for (Element link : links) {
Song song = new Song();
String linkHref = link.attr("href");
String linkText = link.text();
song.setSongUrl(linkHref);
song.setTitle(linkText);
list.add(song);
System.out.println(linkHref + "---" + linkText);
}
return list;
}
/**
* 获取歌曲的封面图片地址
* @param song
* @return
* @throws IOException
*/
public static Song getXiamiSongDetail(Song song) throws IOException {
Document doc = Jsoup
.connect("http://www.xiami.com" + song.getSongUrl())
.data("query", "Java").userAgent("Mozilla")
.cookie("auth", "token")
.timeout(6000)
.get();
Element e = doc.getElementById("albumCover");
Element img = e.select("img").get(0);
String imagSrc = img.attr("src");
System.out.print(imagSrc);
song.setImage(imagSrc);
return song;
}
/**
* 获取歌曲的播放地址
* @param songUrl eg:/song/1769908356
* @return
*/
public static String getFlashPlayerUrl(String songUrl) {
String[] d = songUrl.split("/");
System.out.print(d.length);
return "http://www.xiami.com/widget/0_" + d[2] + "/singlePlayer.swf";
}
public static void main(String args[]) throws IOException {
/*
* Document doc =
* Jsoup.connect("http://www.xiami.com/ajax/search-index")
*
* .data("query", "Java").data("key", "爱")
*
* .userAgent("Mozilla")
*
* .cookie("auth", "token")
*
* .timeout(6000)
*
* .get(); String content = doc.html();
*
* content = content.substring(content.indexOf("
- ") + 4,
* content.indexOf("
"));*
* doc = Jsoup.parse(content); System.out.print(doc.html()); Elements
* links = doc.select("a[href]"); System.out.println("sssssssssss" +
* links.size()); for (Element link : links) { String linkHref =
* link.attr("href"); String linkText = link.text();
* System.out.println(linkHref + "---" + linkText); }
*/
seachXiaMiSongs("爱");
// String s=getFlashPlayerUrl("/song/1769908356");
// System.out.print(s);
// http://www.xiami.com/widget/0_1769908356/singlePlayer.swf
}
}
分享到:
2011-10-28 13:47
浏览 3079
评论