import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
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 dSomeImg {
public void parseUrl(String url) {
try {
Document doc = (Document)Jsoup.connect(url).get(); /*這裏可以直接用jsoup獲取網頁源碼,對於需要登錄驗證之類的網站則需要使用HTTPCLIENT來獲取HTML內容*/
Elements eles = doc.select("img"); /*選取所有IMG標籤元素並打印出來*/
System.out.println(eles);
// Elements pc1 = doc.select("td#prodImageCell>a>img"); /*選取TD標籤下CLASS爲PRODIMAGECELL中的A標籤下的IMG標籤*/
// String wangzhi = pc1.attr("src"); /*得到IMG的鏈接地址*/
// System.out.println(wangzhi);
for(Element ele:eles){
String path = "D:/image/"; /*生成要保存的圖片文件名*/
String name = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
path += name + ".jpg";
String imgUrl = ele.attr("src").toString();
downloadImg(imgUrl,path);
}
} catch (IOException e) {
e.printStackTrace();
}
}
/*下載圖片方法*/
public static void downloadImg(String imgUrl,String path){URL url = null;
try {
url = new URL(imgUrl);
} catch (MalformedURLException e2) {
e2.printStackTrace();
return;
}
InputStream is = null;
try {
is = url.openStream();
} catch (IOException e1) {
e1.printStackTrace();
return;
}
OutputStream os = null;
try{
os = new FileOutputStream(path);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while((bytesRead = is.read(buffer,0,8192))!=-1){
os.write(buffer,0,bytesRead);
}
}catch(FileNotFoundException e){
e.printStackTrace();
return;
} catch (IOException e) {
e.printStackTrace();
return;
}
}
public static void main(String args[]){
String url = "http://tieba.baidu.com/tb/picture/index.html";
dSomeImg dsi = new dSomeImg();
dsi.parseUrl(url);
}
}