</pre><pre name="code" class="java">import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
public class MusicDown {
//get the directory of songs
public static String[] getLastString(String[] changeurl)
{
URL url;
int responsecode;
HttpURLConnection urlConnection;
BufferedReader reader;
String[] line=new String[changeurl.length];
String ss;
try
{
for(int i=0;i<changeurl.length;i++)
{
url=new URL(changeurl[i]);
urlConnection = (HttpURLConnection)url.openConnection();
//获取服务器响应代码
responsecode=urlConnection.getResponseCode();
if(responsecode==200)
{
//得到输入流,即获得了网页的内容
reader=new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"GBK"));
while((ss=reader.readLine())!=null)
{
line[i]=ss.substring(6, ss.length()-2).replace("\\","");
}
}
else{
System.out.println("获取不到网页的源码,服务器响应代码为:"+responsecode);
}
}
}
catch(Exception e)
{
System.out.println("获取不到网页的源码,出现异常:"+e);
}
return line;
}
public static void downLoadSong(String[] songurl,String[] songname) throws IOException
{
URL url;
HttpURLConnection urlConnection;
InputStream in ;
BufferedInputStream bis;
FileOutputStream fw;
BufferedOutputStream bw;
int readLength=0;
try
{
for(int i=0;i<songurl.length;i++)
{
url=new URL(songurl[i]);
urlConnection = (HttpURLConnection)url.openConnection();
in=urlConnection.getInputStream();
bis = new BufferedInputStream(in);
fw=new FileOutputStream("D:\\"+songname[i]+".mp4");
bw=new BufferedOutputStream(fw);
System.out.println(urlConnection.getContentLength());
byte[] byte1=new byte[urlConnection.getContentLength()] ;
while((readLength=bis.read(byte1))!=-1)
{
bw.write(byte1,0,readLength);
}
bw.flush();
}
}
catch(Exception e)
{
System.out.println("获取不到网页的源码,出现异常:"+e);
}
}
public static void main(String[] args) throws Throwable
{
String url="http://music.douban.com/programme/414567";
Document doc= Jsoup.connect(url).get();
Elements codes = doc.body().select("div[class^=song-item]");//直接获取目标行,返回一个类似数组的elements
URL u=new URL(url);
String[] newurlString=new String[codes.size()];
String[] songname=new String[codes.size()];
for(int i=0;i<codes.size();i++)
{ //豆瓣音乐格式
newurlString[i]=u.getProtocol()+"://"+u.getHost()+"/j/songlist/get_song_url?sid="+codes.get(i).attr("data-songid")+"&ssid="+codes.get(i).attr("data-ssid");
songname[i]=codes.get(i).attr("data-title")+"-"+codes.get(i).attr("data-performer");
}
String[] lastString=new String[codes.size()];
lastString=getLastString(newurlString);
downLoadSong(lastString,songname);
}
}
爬取豆瓣首页音乐 批量自动下载基础模型(可方便在此基础上修改)
最新推荐文章于 2022-10-24 10:29:43 发布