如何用java得到网页上的所有链接并存入数据库

来源:百度知道 编辑:UC知道 时间:2024/09/28 11:21:42
我想用java得到一个网页源文件里的所有连接 并存入数据库 哪位能人帮帮忙!!!

用正则表达式小事一桩啦
public static HashSet<String> add(String input) {
HashSet<String> set = new HashSet<String>();
String regex = "<a\\s*href\\s*=\\s*\"?([^\"]+?)\"?>\\s*\\[\\d\\]\\s*</a>";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
while (m.find()) {
set.add(m.group(1));
}
return set;
}

用正则表达式分析吧

给个思路,取<a>标签,把<a>的href得到就行了啦`~
整个源文件做为一个string,循环取到“<a”到“>”结束,取中间的href=" 这里的值 ",substring函数,很简单的,自己试着写写吧`~