package jousp;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class JSoupP {
public static void main(String[] args) throws Exception {
Document document = Jsoup.connect("https://book.douban.com").get();
Elements elements = document.getElementsByTag("img");
Element element = elements.get(0);
String url = element.attr("src");
URL url1 = new URL(url);
HttpURLConnection connection = (HttpURLConnection)url1.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5*1000);
//獲得圖片的輸入流
InputStream inputStream = connection.getInputStream();
byte data[] = readInputStream(inputStream);
//定義圖片位置
File imageFile = new File("D://BeautyGirl.jpg");
//定義輸出流
FileOutputStream outStream = new FileOutputStream(imageFile);
outStream.write(data);
outStream.flush();
outStream.close();
/* public static void main(String[] args) throws Exception {
//new一個URL對象
URL url = new URL("http://img.hexun.com/2011-06-21/130726386.jpg");
//打開鏈接
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//設置請求方式爲"GET"
conn.setRequestMethod("GET");
//超時響應時間爲5秒
conn.setConnectTimeout(5 * 1000);
//通過輸入流獲取圖片數據
InputStream inStream = conn.getInputStream();
//得到圖片的二進制數據,以二進制封裝得到數據,具有通用性
byte[] data = readInputStream(inStream);
//new一個文件對象用來保存圖片,默認保存當前工程根目錄
File imageFile = new File("BeautyGirl.jpg");
//創建輸出流
FileOutputStream outStream = new FileOutputStream(imageFile);
//寫入數據
outStream.write(data);
//關閉輸出流
outStream.close();
} */
}
public static byte[] readInputStream(InputStream inStream) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
//創建一個Buffer字符串
byte[] buffer = new byte[1024];
//每次讀取的字符串長度,如果爲-1,代表全部讀取完畢
int len = 0;
//使用一個輸入流從buffer裏把數據讀取出來
while( (len=inStream.read(buffer)) != -1 ){
//用輸出流往buffer裏寫入數據,中間參數代表從哪個位置開始讀,len代表讀取的長度
outStream.write(buffer, 0, len);
}
//關閉輸入流
inStream.close();
//把outStream裏的數據寫入內存
return outStream.toByteArray();
}
}
jsoup 爬取案例 jsoup
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.