获取网站地址,准备爬取图片
import java.io.File;
import java.io.IOException;
import org.apache.commons.lang.RandomStringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class getDB_film {
public static void main(String[] args) throws IOException {
String url = "http://movie.douban.com/chart";
//获取html
Document doc = Jsoup.connect(url)
.maxBodySize(Integer.MAX_VALUE)
.data("query", "Java")
.cookie("auth", "token")
.userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134")
.timeout(10000)
.post();
//System.out.println(doc);
//逐层分析html
Elements a = doc.select("div[class=\"\"]");
//System.out.println(a);
Elements b= a.select("a[class=nbg]");
//String c = b.attr("href");
//System.out.println(b);
//System.out.println(c);
for(Element element : b){
String video_url = element.attr("href"); // 电影链接地址
String video_name = element.attr("title"); // 电影名字
Elements d= element.select("img");
String img_url = d.attr("src"); //电影图片
//文件名称
StringBuilder stringBuilder = new StringBuilder();
//String filename= RandomStringUtils.randomAlphanumeric(10);
String s = stringBuilder.append("F:\\img").append(File.separator).append(video_name).append(".jpg").toString();
CatchIMG.getImg(img_url,s);
System.out.println("video_name"+video_name);
System.out.println("img_url"+img_url);
System.out.println("video_url"+video_url);
}
}
}
通过 java.net.HttpURLConnection 获取一个URL连接
HttpURLConnection 连接成功返回一个java.io.InputStream,通过InputStream读取图片放入到字节数组buff
通过BufferedOutputStream(new FileOutputStream(new File(“TEST.jpg”))) 形式 将内存buff里的图片数据写入到test.jpg文件中
给出工具类的CatchIMG.java代码如下:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
* @ClassName: CatchIMG
* @Description: 爬取一个指定地址的网络图片
* @author penny
* @date 2017年12月3日 下午9:00:05
*
*/
public class CatchIMG {
/**
*
* @Title: getImg
* @Description: 通过一个url 去获取图片
* @param @param url 图片的连接地址
* @param @throws IOException
* @throws
*/
public static void getImg(String url, String img) throws IOException{
long startTime = System.currentTimeMillis();
URL imgURL = new URL(url.trim());//转换URL
HttpURLConnection urlConn = (HttpURLConnection) imgURL.openConnection();//构造连接
urlConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36");
urlConn.connect();
System.out.println(CatchIMG.class.toString()+":获取连接="+urlConn.getResponseMessage());
if(urlConn.getResponseCode()==200){//返回的状态码是200 表示成功
InputStream ins = urlConn.getInputStream(); //获取输入流,从网站读取数据到 内存中
OutputStream out = new BufferedOutputStream(new FileOutputStream(new File(img)));
int len=0;
byte[] buff = new byte[1024*10];//10k缓冲流 视你内存大小而定咯
while(-1!=(len=(new BufferedInputStream(ins)).read(buff))){//长度保存到len,内容放入到 buff
out.write(buff, 0, len);//将图片数组内容写入到图片文件
// System.out.println(CatchIMG.class.toString()+":"+len+"byte已经写入到文件中,内容: "+new String(buff));
}
urlConn.disconnect();
ins.close();
out.close();
System.out.println(CatchIMG.class.toString()+":获取图片完成,耗时="+((System.currentTimeMillis()-startTime)/1000)+"s");
}
}
/**
* @throws IOException
* @Title: main
* @Description: 测试方法
* @throws
*/
// public static void main(String[] args) throws IOException {
// // CatchIMG.getImg("https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2567198874.jpg");
// }
}