Java讀取網(wǎng)頁內(nèi)容并下載圖片的實(shí)例
Java讀取網(wǎng)頁內(nèi)容并下載圖片的實(shí)例
很多人在第一次了解數(shù)據(jù)采集的時(shí)候,可能無從下手,尤其是作為一個(gè)新手,更是感覺很是茫然,所以,在這里分享一下自己的心得,希望和大家一起分享技術(shù),如果有什么不足,還請(qǐng)大家指正。寫出這篇目的,就是希望大家一起成長,我也相信技術(shù)之間沒有高低,只有互補(bǔ),只有分享,才能使彼此更加成長。
示例代碼:
import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.util.regex.Matcher; import java.util.regex.Pattern; public class GetContentPicture { public void getHtmlPicture(String httpUrl) { URL url; BufferedInputStream in; FileOutputStream file; try { System.out.println("取網(wǎng)絡(luò)圖片"); String fileName = httpUrl.substring(httpUrl.lastIndexOf("/")); String filePath = "./pic/"; url = new URL(httpUrl); in = new BufferedInputStream(url.openStream()); file = new FileOutputStream(new File(filePath+fileName)); int t; while ((t = in.read()) != -1) { file.write(t); } file.close(); in.close(); System.out.println("圖片獲取成功"); } catch (MalformedURLException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public String getHtmlCode(String httpUrl) throws IOException { String content =""; URL uu = new URL(httpUrl); // 創(chuàng)建URL類對(duì)象 BufferedReader ii = new BufferedReader(new InputStreamReader(uu .openStream())); // //使用openStream得到一輸入流并由此構(gòu)造一個(gè)BufferedReader對(duì)象 String input; while ((input = ii.readLine()) != null) { // 建立讀取循環(huán),并判斷是否有讀取值 content += input; } ii.close(); return content; } public void get(String url) throws IOException { String searchImgReg = "(?x)(src|SRC|background|BACKGROUND)=('|\")/?(([\\w-]+/)*([\\w-]+\\.(jpg|JPG|png|PNG|gif|GIF)))('|\")"; String searchImgReg2 = "(?x)(src|SRC|background|BACKGROUND)=('|\")(http://([\\w-]+\\.)+[\\w-]+(:[0-9]+)*(/[\\w-]+)*(/[\\w-]+\\.(jpg|JPG|png|PNG|gif|GIF)))('|\")"; String content = this.getHtmlCode(url); System.out.println(content); Pattern pattern = Pattern.compile(searchImgReg); Matcher matcher = pattern.matcher(content); while (matcher.find()) { System.out.println(matcher.group(3)); this.getHtmlPicture(url+matcher.group(3)); } pattern = Pattern.compile(searchImgReg2); matcher = pattern.matcher(content); while (matcher.find()) { System.out.println(matcher.group(3)); this.getHtmlPicture(matcher.group(3)); } // searchImgReg = // "(?x)(src|SRC|background|BACKGROUND)=('|\")/?(([\\w-]+/)*([\\w-]+\\.(jpg|JPG|png|PNG|gif|GIF)))('|\")"; } public static void main(String[] args) throws IOException { String url = "http://www.baidu.com/"; GetContentPicture gcp = new GetContentPicture(); gcp.get(url); } }
如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
SpringBoot集成Redis并實(shí)現(xiàn)主從架構(gòu)的實(shí)踐
本文主要和大家分享一下在springboot中如何集成redis,并實(shí)現(xiàn)主從架構(gòu),進(jìn)行數(shù)據(jù)的簡單存儲(chǔ),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12Spring?容器初始化?register?與?refresh方法
這篇文章主要介紹了Spring?容器初始化?register?與?refresh方法,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-07-07使用Java自定義注解實(shí)現(xiàn)一個(gè)簡單的令牌桶限流器
限流是在分布式系統(tǒng)中常用的一種策略,它可以有效地控制系統(tǒng)的訪問流量,保證系統(tǒng)的穩(wěn)定性和可靠性,在本文中,我將介紹如何使用Java自定義注解來實(shí)現(xiàn)一個(gè)簡單的令牌桶限流器,需要的朋友可以參考下2023-10-10springboot集成mybatisplus實(shí)例詳解
這篇文章主要介紹了springboot集成mybatisplus實(shí)例詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09