缘由:需要一些声音素材,搜索到站长之家里面有很多素材,但是一个一个下载太麻烦了,而且下载完还要手动改名,甭提多麻烦了。
于是重复的事情交给机器来做好了。
在页面上鼠标移上去可以试听声音的效果,下面有声音的名字,于是想到可以通过解析html,自动下载,保存为对应的文件名。
分析HTML得知,整个在id为"musiclist"的div里,每个音效文件对应里面的一个class为"music_block"的div,再里面的第一个<p>的thumb即是音效文件的下载地址,音效文件名在第2个<p>里包含着。
用JS试着取
var blocks=document.getElementById("musiclist").getElementsByClassName("music_block");
blocks[0].innerText//取得文件名
blocks[0].childNodes[1].getAttribute("thumb")//取得文件下载地址
Document doc = Jsoup.connect(pageURL).get();
Elements blocks = doc.getElementById("musiclist").getElementsByClass("music_block");
int length = blocks.size();
for(int i = 0;i<length;i++){
String filename = blocks.get(i).text();
String downloadUrl = blocks.get(i).child(0).attr("thumb");
}
最后使用Java的URL的读取流写入文件即完成下载
File f = new File("filename");
FileOutputStream out = new FileOutputStream(f);
InputStream is = new URL(downloadUrl ).openStream();
byte[] buf = new byte[1024];
int len = 0;
while((len=is.read(buf))>0){
out.write(buf, 0, len);
}
out.flush();
out.close();
is.close();
package jsoup;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class Download {
/**
*
* @param pageURL e.g. http://sc.chinaz.com/yinxiao/index_2.html
* @return true成功, false失败
*/
public static boolean downloadYinXiao(String pageURL){
System.out.println("任务开始<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
try {
Map
map = new HashMap
();
Document doc = Jsoup.connect(pageURL).get();
System.out.println("Parse url:"+pageURL);
System.out.println("title:"+doc.title());
Element musicList = doc.getElementById("musiclist");
if(musicList==null){
System.out.println("Found node failed: musiclist");
return false;
}
Elements blocks = musicList.getElementsByClass("music_block");
int length = blocks.size();
System.out.println("Found "+length+" yinxiao resources.");
for(int i = 0;i
> entrySet = map.entrySet();
for(Entry
o: entrySet){
System.out.print("Download "+ o.getKey()+"...");
boolean result = download(o.getKey(), o.getValue());
if(result){
System.out.println("OK.");
}else{
System.out.println("Failed. Url:"+o.getValue());
}
}
System.out.println("下载完毕");
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>任务完毕");
return true;
} catch (IOException e) {
e.printStackTrace();
System.err.println("执行出错");
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>任务失败");
}
return false;
}
/**
* 取得文件扩展名
* @param str
* @return
*/
public static String getSuffix(String str){
int lastDotIndex = str.lastIndexOf('.');
if(lastDotIndex==-1){
return ".mp3";
}
String suffix = str.substring(lastDotIndex);
if(suffix.length()!=4){
System.out.println("strange ext:" + suffix +" in " + str);
}
return suffix;
}
/**
*
* @param filename 文件名
* @param url 文件下载地址
* @return true下载成功,false下载失败
*/
public static boolean download(String filename, String url){
//默认把下载的文件保存在download文件夹下
return download("download", filename, url);
}
/**
*
* @param folder 文件保存的目录
* @param filename 文件名
* @param url 文件下载地址
* @return true下载成功,false下载失败
*/
public static boolean download(String folder, String filename, String url){
File dir = new File(folder);
if(!dir.isDirectory()){
dir.mkdirs();
}
File f = new File(dir, filename + getSuffix(url));
try {
FileOutputStream out = new FileOutputStream(f);
InputStream is = new URL(url).openStream();
byte[] buf = new byte[1024];
int len = 0;
while((len=is.read(buf))>0){
out.write(buf, 0, len);
}
out.flush();
out.close();
is.close();
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
/**
*
* @param pageURL URL模版,需要替换的地方用一个问号代替
* e.g. http://sc.chinaz.com/yinxiao/index_?.html
* @param min 代替问号的最小值 e.g. 2
* @param max 代替问号的最大值 e.g. 10
*/
public static void batchDownloadYinXiao(String pageURL, int min, int max){
int i = 0;
//最大出错次数,达到则终止
int ERR_MAX_CNT = 10;
int errCnt = 0;
for(i=min;i<=max;i++){
String url = pageURL.replace("?", String.valueOf(i));
if(!downloadYinXiao(url)){
++errCnt;
}
if(errCnt>=ERR_MAX_CNT){
System.out.println("自动停止。超过最大出错次数:"+ERR_MAX_CNT+", 当前解析地址为: "+url);
return;
}
}
System.out.println("批处理下载音效完毕。出错次数: "+errCnt);
}
public static void main(String[] args) {
// Download.downloadYinXiao("http://sc.chinaz.com/tag_yinxiao/LeQi.html");
Download.batchDownloadYinXiao("http://sc.chinaz.com/yinxiao/index_?.html", 1, 10);
}
}