本帖最后由 u014301433 于 2014-05-05 10:06:44 编辑

解决方案 »

  1.   

    对正则表达式不是很熟悉,不过写了个示例,自己参考下吧:
    示例:import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class RexTest { public static void main(String[] args) {
    String str = getOneHtml("E:/test.html");
    Pattern p = Pattern.compile("(?<=<div class=\"RTit clearfix\"><em>).*(?=</em><span class=\"zjTit cGray\">)");
    Matcher m = p.matcher(str);
    while (m.find())
    System.out.println(m.group()); } public static String getOneHtml(String htmlFile){
            String temp;
            StringBuffer sb = new StringBuffer();
            try {
                File file = new File(htmlFile);
                BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
                while ((temp = in.readLine()) != null) {
                    sb.append(temp.trim());
                }
                in.close();
            }catch (Exception e){
                e.printStackTrace();
            }
            return sb.toString();
        }}<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <div class="RTit clearfix">
              <em>掌趣科技(300315)分红派息记录</em>
              <span class="zjTit cGray"><a href='tab_fhpxya.php?symbol=300315'>分红派息预案</a></span>
            </div>
           <table width="706" border="0" cellspacing="0" cellpadding="0" class="tab01">
           </table>
    </body>
    </html>
      

  2.   

    对正则表达式不是很熟悉,不过写了个示例,自己参考下吧:
    示例:import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class RexTest { public static void main(String[] args) {
    String str = getOneHtml("E:/test.html");
    Pattern p = Pattern.compile("(?<=<div class=\"RTit clearfix\"><em>).*(?=</em><span class=\"zjTit cGray\">)");
    Matcher m = p.matcher(str);
    while (m.find())
    System.out.println(m.group()); } public static String getOneHtml(String htmlFile){
            String temp;
            StringBuffer sb = new StringBuffer();
            try {
                File file = new File(htmlFile);
                BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
                while ((temp = in.readLine()) != null) {
                    sb.append(temp.trim());
                }
                in.close();
            }catch (Exception e){
                e.printStackTrace();
            }
            return sb.toString();
        }}<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <div class="RTit clearfix">
              <em>掌趣科技(300315)分红派息记录</em>
              <span class="zjTit cGray"><a href='tab_fhpxya.php?symbol=300315'>分红派息预案</a></span>
            </div>
           <table width="706" border="0" cellspacing="0" cellpadding="0" class="tab01">
           </table>
    </body>
    </html>

    你的代码用截取的网页源码片断测试可以,但输入完整的URL测试不成功。
    我的测试URL是:http://app.finance.ifeng.com/data/stock/tab_fhpxjl.php?symbol=300315
    麻烦你帮我看下
      

  3.   

    对正则表达式不是很熟悉,不过写了个示例,自己参考下吧:
    示例:import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class RexTest { public static void main(String[] args) {
    String str = getOneHtml("E:/test.html");
    Pattern p = Pattern.compile("(?<=<div class=\"RTit clearfix\"><em>).*(?=</em><span class=\"zjTit cGray\">)");
    Matcher m = p.matcher(str);
    while (m.find())
    System.out.println(m.group()); } public static String getOneHtml(String htmlFile){
            String temp;
            StringBuffer sb = new StringBuffer();
            try {
                File file = new File(htmlFile);
                BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
                while ((temp = in.readLine()) != null) {
                    sb.append(temp.trim());
                }
                in.close();
            }catch (Exception e){
                e.printStackTrace();
            }
            return sb.toString();
        }}<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <div class="RTit clearfix">
              <em>掌趣科技(300315)分红派息记录</em>
              <span class="zjTit cGray"><a href='tab_fhpxya.php?symbol=300315'>分红派息预案</a></span>
            </div>
           <table width="706" border="0" cellspacing="0" cellpadding="0" class="tab01">
           </table>
    </body>
    </html>

    你的代码用截取的网页源码片断测试可以,但输入完整的URL测试不成功。
    我的测试URL是:http://app.finance.ifeng.com/data/stock/tab_fhpxjl.php?symbol=300315
    麻烦你帮我看下你改下程序就可以访问网页,你用下面的方法替换:public static String getHtmlFromURL(String htmlFile){
            URL url;
    String temp;
            StringBuffer sb = new StringBuffer();
            try {
                url = new URL(htmlFile);
                BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8"));
                while ((temp = in.readLine()) != null) {
                    sb.append(temp.trim());
                }
                in.close();
            }catch (Exception e){
                e.printStackTrace();
            }
            return sb.toString();
        }