如何利用java和nutch2.3抓取网页
1个回答
2015-03-29 · 知道合伙人影视综艺行家
关注
展开全部
利用java抓取网页上的所有图片:
用两个正则表达式:
1、匹配html中img标签的正则:<img.*src=(.*?)[^>]*?>
2、匹配img标签中得src中http路径的正则:http:\"?(.*?)(\"|>|\\s+)
实现:
[java] view plaincopyprint?
package org.swinglife.main;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
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;
/***
* java抓取网络图片
* @author swinglife
*
*/
public class CatchImage {
// 地址
private static final String URL = "http://www.csdn.net";
// 编码
private static final String ECODING = "UTF-8";
// 获取img标签正则
private static final String IMGURL_REG = "<img.*src=(.*?)[^>]*?>";
// 获取src路径的正则
private static final String IMGSRC_REG = "http:\"?(.*?)(\"|>|\\s+)";
public static void main(String[] args) throws Exception {
CatchImage cm = new CatchImage();
//获得html文本内容
String HTML = cm.getHTML(URL);
//获取图片标签
List<String> imgUrl = cm.getImageUrl(HTML);
//获取图片src地址
List<String> imgSrc = cm.getImageSrc(imgUrl);
//下载图片
cm.Download(imgSrc);
}
/***
* 获取HTML内容
*
* @param url
* @return
* @throws Exception
*/
private String getHTML(String url) throws Exception {
URL uri = new URL(url);
URLConnection connection = uri.openConnection();
InputStream in = connection.getInputStream();
byte[] buf = new byte[1024];
int length = 0;
StringBuffer sb = new StringBuffer();
while ((length = in.read(buf, 0, buf.length)) > 0) {
sb.append(new String(buf, ECODING));
}
in.close();
return sb.toString();
}
/***
* 获取ImageUrl地址
*
* @param HTML
* @return
*/
private List<String> getImageUrl(String HTML) {
Matcher matcher = Pattern.compile(IMGURL_REG).matcher(HTML);
List<String> listImgUrl = new ArrayList<String>();
while (matcher.find()) {
listImgUrl.add(matcher.group());
}
return listImgUrl;
用两个正则表达式:
1、匹配html中img标签的正则:<img.*src=(.*?)[^>]*?>
2、匹配img标签中得src中http路径的正则:http:\"?(.*?)(\"|>|\\s+)
实现:
[java] view plaincopyprint?
package org.swinglife.main;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
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;
/***
* java抓取网络图片
* @author swinglife
*
*/
public class CatchImage {
// 地址
private static final String URL = "http://www.csdn.net";
// 编码
private static final String ECODING = "UTF-8";
// 获取img标签正则
private static final String IMGURL_REG = "<img.*src=(.*?)[^>]*?>";
// 获取src路径的正则
private static final String IMGSRC_REG = "http:\"?(.*?)(\"|>|\\s+)";
public static void main(String[] args) throws Exception {
CatchImage cm = new CatchImage();
//获得html文本内容
String HTML = cm.getHTML(URL);
//获取图片标签
List<String> imgUrl = cm.getImageUrl(HTML);
//获取图片src地址
List<String> imgSrc = cm.getImageSrc(imgUrl);
//下载图片
cm.Download(imgSrc);
}
/***
* 获取HTML内容
*
* @param url
* @return
* @throws Exception
*/
private String getHTML(String url) throws Exception {
URL uri = new URL(url);
URLConnection connection = uri.openConnection();
InputStream in = connection.getInputStream();
byte[] buf = new byte[1024];
int length = 0;
StringBuffer sb = new StringBuffer();
while ((length = in.read(buf, 0, buf.length)) > 0) {
sb.append(new String(buf, ECODING));
}
in.close();
return sb.toString();
}
/***
* 获取ImageUrl地址
*
* @param HTML
* @return
*/
private List<String> getImageUrl(String HTML) {
Matcher matcher = Pattern.compile(IMGURL_REG).matcher(HTML);
List<String> listImgUrl = new ArrayList<String>();
while (matcher.find()) {
listImgUrl.add(matcher.group());
}
return listImgUrl;
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询