一:下載圖片html
public static void main(String[] args) { try { //屬性 Properties p = System.getProperties() ; p.getProperty("proxy","true" ) ;//代理 p.getProperty("http.proxyHost", "118.254.147.6") ;//代理ip p.getProperty("http.proxyPort","3128" ) ;//代理端口 Map<String,String> map = new HashMap<String,String>() ; //模擬瀏覽器訪問 map.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8") ; map.put("Accept-Encoding", "gzip, deflate") ; map.put("Accept-Language", "zh-CN,zh;q=0.9") ; map.put("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3253.3 Safari/537.36") ; //根據地址,獲得整個頁面 Document d = Jsoup.connect("http://www.ivsky.com/tupian/ziranfengguang").headers(map).get() ; //獲取須要的元素 Elements e = d.select(".ali li img") ; //遍歷元素,獲取須要的數據 for (Element element : e) { //獲得圖片地址 String path = element.attr("src") ; //建立URL對象 URL url = new URL(path) ; //打開連接 HttpURLConnection h = (HttpURLConnection) url.openConnection() ; //設置請求方式爲"GET" h.setRequestMethod("GET"); //超時響應時間爲5秒 h.setConnectTimeout(5000); //經過輸入流獲取圖片數據 InputStream in = h.getInputStream() ; //獲得圖片的二進制數據,以二進制封裝獲得數據,具備通用性 byte[] b = getInputStream(in) ; //保存位置,圖片名字 File f = new File("f:\\小說下載\\圖片.jpg") ; //建立輸出流 OutputStream out = new FileOutputStream(f) ; //寫入數據 out.write(b); //關閉輸出流 out.close(); } } catch (IOException e) { e.printStackTrace(); } } //獲得圖片的二進制數據 public static byte[] getInputStream(InputStream in){ //建立緩衝區 byte[] b = new byte[1024] ; //建立輸出流 ByteArrayOutputStream out = new ByteArrayOutputStream() ; int n = 0 ; try { //循環讀取數據 while((n=in.read(b))!=-1){ //寫入數據到byte[]中 out.write(b, 0, n); } } catch (IOException e) { e.printStackTrace(); }finally{ try { in.close(); } catch (IOException e) { e.printStackTrace(); } } return out.toByteArray() ; }
二:下載小說web
Properties p = System.getProperties() ; p.getProperty("proxy","true" ) ; p.getProperty("http.proxyHost", "118.254.147.6") ; p.getProperty("http.proxyPort","3128" ) ; try { Map<String,String> map = new HashMap<String,String>() ; map.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8") ; map.put("Accept-Encoding", "gzip, deflate") ; map.put("Accept-Language", "zh-CN,zh;q=0.9") ; map.put("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3253.3 Safari/537.36") ; Document d = Jsoup.connect("http://www.23us.so/files/article/html/17/17157/index.html") .headers(map) .get(); Elements e = d.select(".L") ; for (Element element : e) { String url = element.select("a").attr("href"); Document document = Jsoup.connect(url).get() ; String content = document.select("#contents").text() ; String title = document.select("#a_main h1").text() ; //小說保存路徑 String path = "f:\\小說下載\\"+title+".txt" ; //建立文件 File file = new File(path) ; if(!file.exists()){ //文件不存在就建立 file.getParentFile().mkdirs(); }
file.createNewFile() ; //建立輸出流 OutputStream out = new FileOutputStream(file) ; //獲得標題的二進制數據 byte[] b = title.getBytes(); //輸出標題到文件中 out.write(b); //獲得內容的二進制 byte[] by = content.getBytes() ; //輸出內容到文件中 out.write(by); } } catch (IOException e) { e.printStackTrace(); }