public class FindHttp {
private static int count = 0;//记录链接个数
/**
* @param args
*/
public static void main(String[] args) {
try {
BufferedReader buf = new BufferedReader(new FileReader("http.htm"));
String href = "";
while ((href = buf.readLine()) != null) {
findHref(href);
}
} catch (IOException ex) {
Logger.getLogger(FindHttp.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(count);
}
public static void findHref(String href) {
// Pattern p = Pattern.compile("http://[a-zA-Z0-9]+.[a-zA-Z0-9]+.[a-zA-Z]+?[/.?[/S|/s]]+");
// Pattern p = Pattern.compile("http://\\w{1,}.\\w{1,}.\\w{1,}");
// Pattern p = Pattern.compile("http://[\\w{1,}.]+[/\\w{1,}]+");
Pattern p = Pattern.compile("http://[\\w{1,}.]+[/[a-zA-Z0-9&?=-_.]]+(?=\")");
Matcher m = p.matcher(href);
while (m.find()) {
count++;
System.out.println(m.group());
}
}
}
说明:虽然能实现抓取连接的功能,但是也只是抓一部分,测试百度新闻首页的时候只抓取到217个连接,还有的没有抓到,自己能力有限,实在是实现不了了
用正则表达式抓取网络连接的简单实现
关注
打赏