“校车是用来运送学生往返学校的一种交通工具。是集体运输工具之一,收月费或年费。可能是校方聘请司机自行经营,或是与地区性的公车业者签约租用的学生专车。”
这是校车在wikipedia上的解释。我的目的就是抓取这句话。
但是它在网页源代码是这样的:
“<p><b>校车</b>是用来运送<a href="/wiki/%E5%AD%B8%E7%94%9F" title="学生" class="mw-redirect">学生</a>往返学校的一种<a href="/wiki/%E4%BA%A4%E9%80%9A%E5%B7%A5%E5%85%B7" title="交通工具" class="mw-redirect">交通工具</a>。是<a href="/w/index.php?title=%E9%9B%86%E9%AB%94%E9%81%8B%E8%BC%B8%E5%B7%A5%E5%85%B7&amp;action=edit&amp;redlink=1" class="new" title="集体运输工具">集体运输工具</a>之一,收<a href="/w/index.php?title=%E6%9C%88%E8%B2%BB&amp;action=edit&amp;redlink=1" class="new" title="月费">月费</a>或<a href="/w/index.php?title=%E5%B9%B4%E8%B2%BB&amp;action=edit&amp;redlink=1" class="new" title="年费">年费</a>。可能是校方聘请司机自行经营,或是与地区性的公车业者签约租用的学生专车。</p>”请问有方法还原成“校车是用来运送学生往返学校的一种交通工具。是集体运输工具之一,收月费或年费。可能是校方聘请司机自行经营,或是与地区性的公车业者签约租用的学生专车。”这句话吗?

解决方案 »

  1.   


    public static void main(String[] args) throws IOException {
    String str="“<p><b>校车</b>是用来运送<a href='/wiki/%E5%AD%B8%E7%94%9F' title='学生' class='mw-redirect'>" +
    "学生</a>往返学校的一种<a href='/wiki/%E4%BA%A4%E9%80%9A%E5%B7%A5%E5%85%B7' title='交通工具'" +
    " class='mw-redirect'>交通工具</a>。是" +
    "<a href='/w/index.php?title=%E9%9B%86%E9%AB%94%E9%81%8B%E8%BC%B8%E5%B7%A5%E5%85%B7&amp;action=edit&amp;redlink=1' class='new' " +
    "title='集体运输工具'>集体运输工具</a>之一,收<a href='/w/index.php?title=%E6%9C%88%E8%B2%BB&amp;action=edit&amp" +
    ";redlink=1' class='new' title='月费'>月费</a>或<a href='/w/index.php?title=%E5%B9%B4%E8%B2%BB&amp;" +
    "action=edit&amp;redlink=1' class='new' title='年费'>年费</a>。" +
    "可能是校方聘请司机自行经营,或是与地区性的公车业者签约租用的学生专车。</p>'"; String regex="[\u3E00-\u9FA5,。]*";
    Pattern pattern =Pattern.compile(regex);
    Matcher matcher =  pattern.matcher(str);
    String s="";
    while(matcher.find()){
    s+=matcher.group();
    }
    System.out.println(s);

    }
      

  2.   

    System.out.println(str.replaceAll("<.+?>", ""));
      

  3.   

    正则可以搞定
    不过htmlparser估计更好用
      

  4.   


    public static void main(String[] args) throws IOException {
    String str="“<p><b>校车</b>是用来运送<a href='/wiki/%E5%AD%B8%E7%94%9F' title='学生' class='mw-redirect'>" +
    "学生</a>往返学校的一种<a href='/wiki/%E4%BA%A4%E9%80%9A%E5%B7%A5%E5%85%B7' title='交通工具'" +
    " class='mw-redirect'>交通工具</a>。是" +
    "<a href='/w/index.php?title=%E9%9B%86%E9%AB%94%E9%81%8B%E8%BC%B8%E5%B7%A5%E5%85%B7&amp;action=edit&amp;redlink=1' class='new' " +
    "title='集体运输工具'>集体运输工具</a>之一,收<a href='/w/index.php?title=%E6%9C%88%E8%B2%BB&amp;action=edit&amp" +
    ";redlink=1' class='new' title='月费'>月费</a>或<a href='/w/index.php?title=%E5%B9%B4%E8%B2%BB&amp;" +
    "action=edit&amp;redlink=1' class='new' title='年费'>年费</a>。" +
    "可能是校方聘请司机自行经营,或是与地区性的公车业者签约租用的学生专车。</p>'";

    String regex="title='[\u3E00-\u9FA5]*'";//先把title='XXX'找到
    Pattern pattern =Pattern.compile(regex);
    Matcher matcher =  pattern.matcher(str);

    while(matcher.find()){
    str=str.replaceAll(matcher.group(), "");//替换到title='XXX'
    }

    regex="[\u3E00-\u9FA5,。]*";//再找中文、句号、逗号……
    pattern =Pattern.compile(regex);
    matcher =  pattern.matcher(str);
    String s="";

    while(matcher.find()){
    s+=matcher.group();
    }
    System.out.println(s);

    }刚没注意title='学生' ,这种中文不应出现在返回的结果中。
      

  5.   

    仅针对wikipedia?
        String str="<p><b>校车</b>是用来运送<a href='/wiki/%E5%AD%B8%E7%94%9F' title='学生' class='mw-redirect'>" +
        "学生</a>往返学校的一种<a href='/wiki/%E4%BA%A4%E9%80%9A%E5%B7%A5%E5%85%B7' title='交通工具'" +
        " class='mw-redirect'>交通工具</a>。是" +
        "<a href='/w/index.php?title=%E9%9B%86%E9%AB%94%E9%81%8B%E8%BC%B8%E5%B7%A5%E5%85%B7&amp;action=edit&amp;redlink=1' class='new' " +
        "title='集体运输工具'>集体运输工具</a>之一,收<a href='/w/index.php?title=%E6%9C%88%E8%B2%BB&amp;action=edit&amp" +
        ";redlink=1' class='new' title='月费'>月费</a>或<a href='/w/index.php?title=%E5%B9%B4%E8%B2%BB&amp;" +
        "action=edit&amp;redlink=1' class='new' title='年费'>年费</a>。" +
        "可能是校方聘请司机自行经营,或是与地区性的公车业者签约租用的学生专车。</p>";
        System.out.println(str.replaceAll("<.*?>", ""));