注 : 代码1的正则将会在html里循环查找,代码2的正则只会查找第一个匹配的项 代码1: package utils.net; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 学习使用 * * @author loocao * * (代码无版权,欢迎强奸) */ public class LoadWebSite1 { /** * @param args */ public static void main(String[] args) { String url = "http://www.qx121.com/web/plug/weather2.asp"; String html = getHtml(url); System.out.println(html); System.out.println(); String s[] = queryString(html, "<a target=/"_blank/" href=/".{20}/" >(.{2,100})</a>"); for (String str : s) { System.out.println(str); } } /** * 获取url的HTML内容 <br/> * URL的使用方法是在网上找到的,了解更多,请参考Apache Client * * @param url * @return */ public static String getHtml(String url) { StringBuffer html = new StringBuffer(); try { URL u = new URL(url); BufferedReader reader = new BufferedReader(new InputStreamReader(u .openStream())); String temp; while ((temp = reader.readLine()) != null) { html.append(temp); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return html.toString(); } /** * 根据regex查找字符串 * * @param html * @param regex * @return */ public static String[] queryString(String html, String regex) { Pattern p = Pattern.compile(regex); Matcher m = p.matcher(html); List<String> list = new ArrayList<String>(); while (m.find()) { for (int i = 0; i < m.groupCount(); i++) { list.add(m.group(i + 1)); } } return list.toArray(new String[list.size()]); } } 代码2: package utils.net; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 学习使用 * * @author loocao * * (代码无版权,欢迎强奸) */ public class LoadWebSite2 { /** * @param args */ public static void main(String[] args) { String url = "http://www.qx121.com/web/plug/weather2.asp"; String html = getHtml(url); System.out.println(html); System.out.println(); String[] s = queryString( html, "<a target=/"_blank/" href=/".{0,255}/" >" + "(.{4,20})</a></td> <td height=/"20/" " + "align=/"center/" valign=/"middle/">" + "<a target=/"_blank/" href=/".{0,255}/" >" + "(.{2,10})</a></td> <td height=/"20/" " + "align=/"center/" valign=/"middle/">" + "<a target=/"_blank/" href=/".{0,255}/" >" + "(.{4,100})</a>"); for (String str : s) { System.out.println(str); } } /** * 获取url的HTML内容 <br/> * URL的使用方法是在网上找到的,了解更多,请参考Apache Client * * @param url * @return */ public static String getHtml(String url) { StringBuffer html = new StringBuffer(); try { URL u = new URL(url); BufferedReader reader = new BufferedReader(new InputStreamReader(u .openStream())); String temp; while ((temp = reader.readLine()) != null) { html.append(temp); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return html.toString(); } /** * 根据regex查找字符串 * * @param html * @param regex * @return */ public static String[] queryString(String html, String regex) { Pattern p = Pattern.compile(regex); Matcher m = p.matcher(html); String s[] = new String[m.groupCount()]; if (m.find()) { for (int i = 0; i < s.length; i++) { s[i] = m.group(i + 1); } } return s; } }