<div>
<a target="_self" href="http://news.qq.com/" class="b">新闻</a><a target="_self" href="http://v.qq.com/">视频</a><a target="_self" href="http://pp.qq.com/">图片</a><a target="_self" href="http://view.news.qq.com/">评论</a><br>
<a target="_self" href="http://finance.qq.com/" class="b">财经</a><a target="_self" href="http://finance.qq.com/stock/">股票</a><a target="_self" href="http://finance.qq.com/hk/">港股</a><a target="_self" href="http://finance.qq.com/fund/">基金</a></div>
<div class="line"></div>
<div>
<a target="_self" href="http://ent.qq.com/" class="b">娱乐</a><a target="_self" href="http://ent.qq.com/star/">明星</a><a target="_self" href="http://ent.qq.com/movie/">电影</a><a target="_self" href="http://yue.qq.com/">音乐</a><br>
<a target="_self" href="http://sports.qq.com/" class="b">体育</a><a target="_self" href="http://sports.qq.com/nba/" style="font-family:Arial;padding-top:0;padding-bottom:3px;">NBA</a><a href="http://sports.qq.com/lottery/" target="_self">彩票</a><a href="http://2012.qq.com/" target="_self">奥运</a></div>
<div class="line"></div>
<div>
<a target="_self" href="http://auto.qq.com/" class="b">汽车</a><a target="_self" href="http://data.auto.qq.com/car_brand/index.shtml">车型</a><a target="_self" href="http://house.qq.com/">房产</a><a target="_self" href="http://hea.qq.com/">家电</a><br>
<a target="_self" href="http://tech.qq.com/" class="b">科技</a><a target="_self" href="http://digi.tech.qq.com/">数码</a><a target="_self" href="http://digi.tech.qq.com/mobile/">手机</a><a target="_self" href="http://download.tech.qq.com/">下载</a></div>
<div class="line"></div>
<div>如上代码:
用程序可以取出红色标记的数据吗??
有代码更好

解决方案 »

  1.   

    这个可以使用html解析器!
    jsoup 是 Java 的HTML 解析器,可直接解析某个URL地址、HTML文本内容。File input = new File("/tmp/input.html");
    Document doc = Jsoup.parse(input, "UTF-8", "http://example.com/");
    Element content = doc.getElementById("content");
    Elements links = content.getElementsByTag("a");
    for (Element link : links) {  
    String linkHref = link.attr("href");  
    String linkText = link.text();
    }也可以模仿这个解析器用循环截取的方式取出你要的链接和文字。不过这个就麻烦多了
      

  2.   

    http://www.oschina.net/p/jsoup
      

  3.   

    如果是HTML文件,我觉得试试当文件读取也没事吧,以href="为标记读到下一个"就是你的URL了,
      

  4.   

    这个貌似也可以达到楼主的要求   public class Test {
    public static void main(String[] args) {
    //截取出字符串中的链接和文字部分---最好用html解析
    String strs2="<a target='_self' href='http://news.qq.com/' class='b'>新闻</a><a target='_self' href='http://v.qq.com/'>视频</a><a target='_self' href='http://pp.qq.com/'>图片</a><a target='_self' href='http://view.news.qq.com/'>评论</a>";
    String len[] = strs2.split("href");
    for(int i=1;i<len.length;i++){
    //System.out.println(len[i]);
    if(len[i].contains("http")){
    //System.out.println(len[i].indexOf(">"));
    //System.out.println(len[i].indexOf("/"));
    String str = len[i].toString().substring((len[i].toString().indexOf("'")+1),(len[i].toString().indexOf("/'")));
    String strval = len[i].toString().substring((len[i].indexOf("'>"))+2,(len[i].indexOf("</")));
    System.out.println(str);
    System.out.println(strval);
       }
      } }
    }
      

  5.   

    一条正则搞定
    String source ="<div>\n" +
                    "<a target=\"_self\" href=\"http://news.qq.com/\" class=\"b\">新闻</a><a target=\"_self\" href=\"http://v.qq.com/\">视频</a><a target=\"_self\" href=\"http://pp.qq.com/\">图片</a><a target=\"_self\" href=\"http://view.news.qq.com/\">评论</a><br>\n" +
                    "<a target=\"_self\" href=\"http://finance.qq.com/\" class=\"b\">财经</a><a target=\"_self\" href=\"http://finance.qq.com/stock/\">股票</a><a target=\"_self\" href=\"http://finance.qq.com/hk/\">港股</a><a target=\"_self\" href=\"http://finance.qq.com/fund/\">基金</a></div>" ;
            Pattern pattern=Pattern.compile("<a[\\s\\S]*?href=\"(.*?)\"[\\s\\S]*?>([\\s\\S]*?)</a>");
            Matcher matcher=pattern.matcher(source);
            while(matcher.find()){
                System.out.println(matcher.group(1)+"=>"+matcher.group(2));
            }
    输出
    http://news.qq.com/=>新闻
    http://v.qq.com/=>视频
    http://pp.qq.com/=>图片
    http://view.news.qq.com/=>评论
    http://finance.qq.com/=>财经
    http://finance.qq.com/stock/=>股票
    http://finance.qq.com/hk/=>港股
    http://finance.qq.com/fund/=>基金
      

  6.   


    我还是觉得这个用html解析器更好点!
      

  7.   

    htmlparser 解析是个不错的开源jar
      

  8.   

    还可以用jsvar urls = [];
    var urlText = [];
    $('A').each(function(index){
     urls.push($(this).attr('href'));
     urlText.push($(this).html()) 
    })
      

  9.   

    $('a[href^=http://]')取出所有以http打头的a标记的href属性的a标记,然后再遍历$('a[href^=http://]').each(function(_a){});
      

  10.   

    直接用jquery
    $(document).ready(function(){
        //输出链接
        alert($(this).attr('href'));
    });