首先,我们分析下 html源代码结构 来到 2011-2012赛季英超球队战绩 页面,右键 点击 '查看源文件' (其它浏览器可能叫源代码或者相关).
我们来看看 其内部的html代码结构 以及我们需要的数据.
其对应的页面数据
这时候,强大的正则表达式派上用场了, 我们需要写几个用来抓取球队数据的正则.
这里需要用到3个正则表达式: 日期正则,2个球队正则(主队和客队)以及比赛结果正则.
String regularDate = "(\d{1,2}\.\d{1,2}\.\d{4})"; //日期正则
String regularTwoTeam = ">[^<>]*</a>"; //球队正则
String regularResult = ">(\d{1,2}-\d{1,2})</TD>"; //比赛结果正则
写好正则, 我们便可以使用该正则来抓取我们想要得到的数据了.
首先,我们写一个GroupMethod类, 里面包含了regularGroup()方法,用于抓取html页面数据.
import java.util.regex.Matcher; import java.util.regex.Pattern; /** * GroupMethod类 用于匹配并抓去 Html上我们想要的内容 * @author SoFlash - 博客园 http://www.cnblogs.com/longwu */ public class GroupMethod { // 传入2个字符串参数 一个是pattern(我们使用的正则) 另一个matcher是html源代码 public String regularGroup(String pattern, String matcher) { Pattern p = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(matcher); if (m.find()) { // 如果读到 return m.group();// 返回捕获的数据 } else { return ""; // 否则返回一个空字符串 } } }
然后在主函数里实施html页面的数据抓取.
Comments NOTHING