欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java正則表達式解析html示例分享

 更新時間:2014年02月26日 09:16:23   投稿:zxhpj  
這篇文章主要介紹了java正則表達式解析html示例,用到獲取url的正則表達式,獲取圖片的正則表達式,需要的朋友可以參考下

復制代碼 代碼如下:

package work;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class chuanboyi {

 public static void main(String[] args){
  // TODO Auto-generated method stub
  StringBuffer html = new StringBuffer();
  HttpClient httpclient = new HttpClient();
  //創(chuàng)建GET方法實例
  GetMethod getMethod = new GetMethod("//www.dbjr.com.cn");
  //使用系統(tǒng)提供的默認恢復策略
  getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
  try{
   //執(zhí)行GET方法
   int statusCode = httpclient.executeMethod(getMethod);
   if(statusCode != HttpStatus.SC_OK){
    System.out.println("Method is wrong " + getMethod.getStatusLine());
   }
   InputStream responseBody = getMethod.getResponseBodyAsStream();
   BufferedReader reader = new BufferedReader(new InputStreamReader(responseBody,"utf-8"));
   String line = reader.readLine();
   while(line != null){
    html.append(line).append("\n");
    line = reader.readLine();
   }
   reader.close();
   //正則表達式
   String regex = "<form name=\"compareForm\"[\\s\\S]+>[\\s\\S]+</form>.*<script.*>";
   String regexa ="(?<=<li>)[\\s\\S]+?(?=</li>)";
   Pattern pattern = Pattern.compile(regex);
         Matcher m = pattern.matcher(html);
         StringBuffer str = new StringBuffer();
         int i = 0;
         while(m.find()){
          str.append(m.group());
         }
         pattern = Pattern.compile(regexa);
         m = pattern.matcher(str);
         while(m.find()){
          attrs(m.group());
          i++;
         }
         System.out.println("共有"+i+"條數(shù)據(jù)!");
  }catch (HttpException e) {
   // TODO: handle exception
   System.out.println("Please check your provided http address!");
   e.printStackTrace();
  }catch (IOException e) {
   // TODO: handle exception
   System.out.println("the line is wrong!");
   e.printStackTrace();
  }finally{
   getMethod.releaseConnection();//釋放鏈接
  }
 }
 public static void attrs(String str){
  
  //獲取url的正則表達式
  String regexURL = "[a-z]+-[0-9]+\\.html";
  //獲取Name的正則表達式
  String regexName = "(?<=title=\")[[\\w-\\s][^x00-xff]]+(?=\")";
  //獲取圖片的正則表達式
  String regexPicture = "images.*\\.jpg";
  
  Pattern patternURL = Pattern.compile(regexURL);
  Pattern patternName = Pattern.compile(regexName);
  Pattern patternPicture = Pattern.compile(regexPicture);
  Matcher mURL = patternURL.matcher(str);
  Matcher mName = patternName.matcher(str);
  Matcher mPicture = patternPicture.matcher(str);
  if(mName.find()){
   System.out.println("名字:"+mName.group());
  }
  if(mURL.find()){
   System.out.println("鏈接:"+mURL.group());
  }
  if(mPicture.find()){
   System.out.println("圖片:"+mPicture.group());
  }
 } 
}

相關(guān)文章

  • Spring?Batch實現(xiàn)批量處理

    Spring?Batch實現(xiàn)批量處理

    本文主要介紹了Spring?Batch進行批量處理,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-06-06
  • Hibernate Validator實現(xiàn)更簡潔的參數(shù)校驗及一個util

    Hibernate Validator實現(xiàn)更簡潔的參數(shù)校驗及一個util

    這篇文章主要介紹了Hibernate Validator實現(xiàn)更簡潔的參數(shù)校驗及一個util,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-05-05
  • springboot在filter中如何用threadlocal存放用戶身份信息

    springboot在filter中如何用threadlocal存放用戶身份信息

    這篇文章主要介紹了springboot中在filter中如何用threadlocal存放用戶身份信息,本文章主要描述通過springboot的filter類,在過濾器中設(shè)置jwt信息進行身份信息保存的方法,需要的朋友可以參考下
    2024-07-07
  • Java 正確地從類路徑中獲取資源

    Java 正確地從類路徑中獲取資源

    Java 有能力從類路徑中查找獲取資源,可將資源放在 CLASSPATH 里,也可打包到 Jar 中。本文將具體講述獲取資源的步驟,感興趣的朋友可以了解下
    2021-05-05
  • 最新評論