使用java中的正则表达式从url中提取一些内容

时间:2011-09-01 19:18:16

标签: java regex

我想从此网址http://www.xyz.com/default.aspx中提取内容,这是我要使用正则表达式提取的以下内容。

String expr = "
What Regular Expression should I use here    
"; 

Pattern patt = Pattern.compile(expr, Pattern.DOTALL | Pattern.UNIX_LINES);
URL url4 = null;

try {
    url4 = new URL("http://www.xyz.com/default.aspx");                  
} catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
System.out.println("Text" +url4);
Matcher m = null;
try {
    m = patt.matcher(getURLContent(url4));
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
System.out.println("Match" +m);

while (m.find()) {
    String stateURL = m.group(1);
    System.out.println("Some Data" +stateURL);
}

public static CharSequence getURLContent(URL url8) throws IOException {
          URLConnection conn = url8.openConnection();
          String encoding = conn.getContentEncoding();
          if (encoding == null) {
            encoding = "ISO-8859-1";
          }
          BufferedReader br = new BufferedReader(new
              InputStreamReader(conn.getInputStream(), encoding));
          StringBuilder sb = new StringBuilder(16384);
          try {
            String line;
            while ((line = br.readLine()) != null) {
              sb.append(line);
              System.out.println(line);
              sb.append('\n');
            }
          } finally {
            br.close();
          }
          return sb;
        }

1 个答案:

答案 0 :(得分:0)

正如@ bkent314所提到的,jsoup是比使用正则表达式更好更清晰的方法。

如果您检查该网站的源代码,您基本上需要此代码段中的内容: -

<div class="smallHd_contentTd">
    <div class="breadcrumb">...</div>
    <h2>Services</h2>
    <p>...</p>
    <p>...</p>
    <p>...</p>
</div>

通过使用jsoup,您的代码可能如下所示: -

Document doc = Jsoup.connect("http://www.ferotech.com/Services/default.aspx").get();

Element content = doc.select("div.smallHd_contentTd").first();

String header = content.select("h2").first().text();

System.out.println(header);

for (Element pTag : content.select("p")) {
    System.out.println(pTag.text());
}

希望这有帮助。

相关问题