亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

java正則表達(dá)式解析html示例分享

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

復(fù)制代碼 代碼如下:

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方法實(shí)例
  GetMethod getMethod = new GetMethod("//chabaoo.cn");
  //使用系統(tǒng)提供的默認(rèn)恢復(fù)策略
  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();
   //正則表達(dá)式
   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的正則表達(dá)式
  String regexURL = "[a-z]+-[0-9]+\\.html";
  //獲取Name的正則表達(dá)式
  String regexName = "(?<=title=\")[[\\w-\\s][^x00-xff]]+(?=\")";
  //獲取圖片的正則表達(dá)式
  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實(shí)現(xiàn)批量處理

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

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

    Hibernate Validator實(shí)現(xiàn)更簡(jiǎn)潔的參數(shù)校驗(yàn)及一個(gè)util

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

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

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

    Java 正確地從類(lèi)路徑中獲取資源

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