爬取豆瓣首页音乐 批量自动下载基础模型(可方便在此基础上修改)

</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);
	
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值