Java中怎么抓取网页中的图片

如题所述

通过httpclient来爬取网站内容,分析当前内容页中的图片‘规则’
抓取一般都是模拟浏览器访问目标网页,通过返回的页面html代码进行分析自己需要的数据
查找规则,例如你爬取的网页 ,看到当前页面显示的图片格式如下<img src="http://www.baidu.com/img/20101025_user.png">
通过解析爬取的网页源代码(html)进行字符串的操作即可,现在有相应的第三方jar包可以帮你更快的完成这部分工作,例如htmlpaser,获取到对应的地址,然后进行保存或下载。
你可以搜索,java爬虫(httpclient)和htmlpaser做更多的了解。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-11-02
我以前写的一个java程序,刚才又试了下,可以的:

/**
第二个将网页中所有图片保存到本地:
package 网页爬虫;
**/

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
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 GetImage {
/**
* @param args
*/
private List<String> imageUrl = new ArrayList<String>();//用于存储图片的url
private int count = 0;//图片计数器
public static void main(String[] args) {
String netUrl = "http://www.qbhwx.com/product/category/?&page=13";//要爬的网页
//String netUrl = "http://sejie.wanxun.org/post/2012-09-25/40039413449";

new GetImage().init(netUrl);
}
public void init(String netUrl)
{
getPage(netUrl);
while(imageUrl.size()!=0)
{
getImage(imageUrl.remove(0));
}
}
//获取网页信息line中的图片url并加入到集合中
public void getImageUrl(String line)
{
String regex = "src=\"http://.{1,}.jpg";
Pattern pat = Pattern.compile(regex);
Matcher matcher=pat.matcher(line);
String str =null;
while(matcher.find())
{
str = matcher.group();
imageUrl.add(str.substring(5));
}
}
//爬取网页中的信息。
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);//判断网页中的jpg图片
}

} catch (MalformedURLException e) {
System.out.println("url异常");
} catch (IOException e) {
System.out.println("url连接异常");
}finally
{
if( mybr != null)
{
try {
mybr.close();
} catch (IOException e) {
System.out.println("读入流关闭异常");
}
}
}
}
//下载该图片!
public void getImage(String imageUrl)
{
InputStream myin = null;
BufferedOutputStream myos = null;
try {
URL myurl = new URL(imageUrl);
URLConnection myconn = myurl.openConnection();
myin = myconn.getInputStream();
myos = new BufferedOutputStream(new FileOutputStream(count+".jpg"));
byte[] buff = new byte[1024];
int num = 0;
while((num = myin.read(buff))!= -1)
{
myos.write(buff, 0, num);
myos.flush();
}
count++;
} catch (MalformedURLException e) {
System.out.println("url异常");
} catch (IOException e) {
System.out.println("url连接异常");
}
finally
{
if( myin != null)
{
try {
myos.close();
} catch (IOException e) {
System.out.println("读入流关闭异常");
}
}
if( myos != null)
{
try {
myos.close();
} catch (IOException e) {
System.out.println("输出流关闭异常");
}
}
}
}

}
第2个回答  2021-07-29

根据java网络编程相关的内容,使用jdk提供的相关类可以得到url对应网页的html页面代码。

针对得到的html代码,通过使用正则表达式即可得到我们想要的内容

比如,我们如果想得到一个网页上所有包括“java”关键字的文本内容,就可以逐行对网页代码进行正则表达式的匹配。最后达到去除html标签和不相关的内容,只得到包括“java”这个关键字的内容的效果。

从网页上爬取图片的流程和爬取内容的流程基本相同,但是爬取图片的步骤会多一步。

需要先用img标签的正则表达式匹配获取到img标签,再用src属性的正则表达式获取这个img标签中的src属性的图片url,然后再通过缓冲输入流对象读取到这个图片url的图片信息,配合文件输出流将读到的图片信息写入到本地即可。

第3个回答  2014-11-01
用htmlparser,里面有方法
第4个回答  推荐于2016-02-02
如下:
public static String do_post(String url, List<NameValuePair> name_value_pair) throws IOException {
        String body = "{}";
        DefaultHttpClient httpclient = new DefaultHttpClient();
        try {
            HttpPost httpost = new HttpPost(url);
            httpost.setEntity(new UrlEncodedFormEntity(name_value_pair, StandardCharsets.UTF_8));
            HttpResponse response = httpclient.execute(httpost);
            HttpEntity entity = response.getEntity();
            body = EntityUtils.toString(entity);
        } finally {
            httpclient.getConnectionManager().shutdown();
        }
        return body;
    }
    public static String do_get(String url) throws ClientProtocolException, IOException {
        String body = "{}";
        DefaultHttpClient httpclient = new DefaultHttpClient();
        try {
            HttpGet httpget = new HttpGet(url);
            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            body = EntityUtils.toString(entity);
        } finally {
            httpclient.getConnectionManager().shutdown();
        }
        return body;
    }

本回答被提问者和网友采纳
相似回答