抓取虾米音乐网站音乐,搜索虾米音乐功能的java实现

可以实现和点点网音乐分享功能暂时还没做界面,先留下代码

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<Song> 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("<ul>") + 4, content
.indexOf("</ul>"));

doc = Jsoup.parse(content);
System.out.print(doc.html());
Elements links = doc.select("a[href]");
System.out.println("sssssssssss" + links.size());
List<Song> 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("<ul>") + 4,
* content.indexOf("</ul>"));
* 
* 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

}

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值