您当前的位置: 首页 >  网络

郭梧悠

暂无认证

  • 5浏览

    0关注

    402博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

用正则表达式抓取网络连接的简单实现

郭梧悠 发布时间:2012-06-14 23:58:36 ,浏览量:5

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个连接,还有的没有抓到,自己能力有限,实在是实现不了了
关注
打赏
1663674776
查看更多评论
立即登录/注册

微信扫码登录

0.0372s