日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java抓取图片_java 抓取网页的图片

發(fā)布時間:2023/12/20 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java抓取图片_java 抓取网页的图片 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

//只能抓取一部分圖片,像折800有些子路徑的一行圖片代碼有好多個img,而且排列不規(guī)律,我的能力根本就沒法截取下來

package test;

import java.io.BufferedOutputStream;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.net.MalformedURLException;

import java.net.URL;

import java.net.URLConnection;

import java.util.ArrayList;

import java.util.List;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class getImageByUrl4 {

/**

* @param args

*/

private List imageUrl = new ArrayList();//用于存儲圖片的url

private int count = 0;//圖片計數(shù)器

public static void main(String[] args) {

String netUrl = "http://www.zhe800.com";//要爬的網(wǎng)頁

new getImageByUrl4().init(netUrl);

}

public void init(String netUrl){

getPage(netUrl);

while(imageUrl.size()!=0)

{

getImage(imageUrl.remove(0));

}

}

//獲取網(wǎng)頁信息line中的圖片url并加入到集合中

public void getImageUrl(String line,String netUrl){

//三種正則表達式

//其他網(wǎng)站的圖片,http開頭如:src = "http://www.ecoc.com:8080/pic/jfjiejf.jpg

//String searchImgReg = "(src|SRC|background|BACKGROUND|background-image|BACKGROUND-IMAGE)=('|\")(http://([\\w-]+\\.)+[\\w-]+(:[0-9]+)*(/+[\\w-]+)*(/+[\\w-]+\\.(jpg|JPG|png|PNG|gif|GIF)))";

String searchImgReg = "(src|SRC|background|BACKGROUND|background-image|BACKGROUND-IMAGE)\\w{0,2}=('|\")http(s)*://.{1,}.(jpg|JPG|png|PNG|gif|GIF)\"\\s";

//項目中的圖片,絕對路徑如:src = "/ecoc/lala/jj/ooellaie.jpg

//String searchImgReg2 = "(src|SRC|background|BACKGROUND|background-image|BACKGROUND-IMAGE)\\w{0,2}=('|\")/*(([\\w-]+/)*([\\w-]+\\.(jpg|JPG|png|PNG|gif|GIF)))('\")";

String searchImgReg2 = "(src|SRC|background|BACKGROUND|background-image|BACKGROUND-IMAGE)\\w{0,2}=('|\")/*(([\\w-]+/)*([\\w-]+\\.(jpg|JPG|png|PNG|gif|GIF)))\"\\s";

try {

Pattern pat = Pattern.compile(searchImgReg);

Matcher matcher=pat.matcher(line);

String str =null;

while(matcher.find())

{

str = matcher.group();

String []sttr = str.split(" ");System.out.println(str);

for(int i = 0;i

String s = sttr[i];

Integer index_denghao = s.indexOf("=")+2;

imageUrl.add(s.substring(index_denghao,s.length()-1));

}

}

pat = Pattern.compile(searchImgReg2);

matcher=pat.matcher(line);

while(matcher.find())

{

str = matcher.group(); System.out.println(str);

String []sttr = str.split(" ");

for(int i = 0;i

String s = sttr[i];System.out.println(s);

Integer index_denghao = s.indexOf("=")+2;

Integer index_2 = netUrl.indexOf("/", 8);

if(index_2==-1)

index_2 = netUrl.length();

imageUrl.add(netUrl.substring(0, index_2)+"/"+s.substring(index_denghao,s.length()-1));

}

}

} catch (Exception e) {

}

}

//爬取網(wǎng)頁中的信息。

public void getPage(String netUrl){

BufferedReader mybr = null;

try {

URL myurl = new URL(netUrl);

URLConnection myconn = myurl.openConnection();

InputStream myin = myconn.getInputStream();

mybr = new BufferedReader(new InputStreamReader(myin,"UTF-8"));

String line;

while((line = mybr.readLine())!= null)

{

getImageUrl(line,netUrl);//判斷網(wǎng)頁中的jpg圖片

}

} catch (MalformedURLException e) {

System.out.println("getPage url異常");

} catch (IOException e) {

System.out.println("url連接異常");

e.printStackTrace();

}finally {

if( mybr != null)

{

try {

mybr.close();

} catch (IOException e) {

System.out.println("讀入流關(guān)閉異常");

}

}

}

}

//下載該圖片!

public void getImage(String imageUrl){

InputStream myin = null;

BufferedOutputStream myos = null;

try {

File file = new File("H:\\pic\\");

File[] files = file.listFiles();

for (File file2 : files) {

Integer fileName = Integer.valueOf(file2.getName().substring(0, file2.getName().indexOf(".")));

if(count

count = fileName;

}

}

URL myurl = new URL(imageUrl);

URLConnection myconn = myurl.openConnection();

myin = myconn.getInputStream();

myos = new BufferedOutputStream(new FileOutputStream("H:\\pic\\"+(++count)+".jpg"));

byte[] buff = new byte[1024];

int num = 0;

while((num = myin.read(buff))!= -1)

{

myos.write(buff, 0, num);

myos.flush();

}

} catch (MalformedURLException e) {

System.out.println("getImage url異常");

e.printStackTrace();

} catch (IOException e) {

System.out.println("下載圖片url連接異常");

e.printStackTrace();

}

finally{

if( myin != null){

try {

myin.close();

} catch (IOException e) {

System.out.println("讀入流關(guān)閉異常");

}

}

if( myos != null){

try {

myos.close();

} catch (IOException e) {

System.out.println("輸出流關(guān)閉異常");

}

}

}

}

}

總結(jié)

以上是生活随笔為你收集整理的java抓取图片_java 抓取网页的图片的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。