/**
* @version
* @description
*/
package ioTest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author 26248
*
*/
public class URLTestDemo2 {
public static void main(String[] args) throws IOException {
StringBuffer buf=new StringBuffer();
URL url=new URL("http://www.163.com");
InputStreamReader is=new InputStreamReader(url.openStream(),"gbk");
BufferedReader bs=new BufferedReader(is);
String test=null;
Pattern pa=Pattern.compile("href=\"[(http)|(https)].+?\"");
while((test=bs.readLine())!=null) {
buf.append(test);
}
// System.out.println(buf.toString());
Matcher ma=pa.matcher(buf.toString());
while(ma.find()) {
System.out.println(ma.group());
}
is.close();
bs.close();
}
}
结果展示(部分):
href=”http://img1.cache.netease.com”
href=”http://img1.cache.netease.com/www/logo/logo-ipad-icon.png”
href=”http://img2.cache.netease.com/f2e/www/index20170701/collect/head~YeB9lpphIPQQ.css”
href=”http://www.163.com/”
href=”http://www.163.com/#f=topnav”
href=”http://m.163.com/newsapp/#f=topnav”
href=”http://open.163.com/#f=topnav”
href=”http://live.ent.163.com/download?f=163.homeNavApp”
href=”https://hongcai.163.com/?from=pcsy-button”
href=”http://u.163.com/aosoutbdbd8”
href=”http://mail.163.com/client/dl.html?from=mail46”
href=”http://163.lu/gOcz51”
href=”http://study.163.com/client/download.htm?from=163app&utm_source=163.com&utm_medium=web_app&utm_campaign=business”
href=”http://news.163.com”
href=”http://news.163.com/domestic”
href=”http://news.163.com/world”
href=”http://d.news.163.com”
href=”http://news.163.com/photo”
href=”http://view.163.com”
href=”http://discovery.163.com”
href=”http://war.163.com”
href=”http://news.163.com/localnews/”
href=”http://sports.163.com”
href=”http://sports.163.com/nba”
href=”http://sports.163.com/cba”
href=”http://sports.163.com/allsports”