主頁 > 知識庫 > Java用正則表達式如何讀取網(wǎng)頁內容

Java用正則表達式如何讀取網(wǎng)頁內容

熱門標簽:宿遷智能外呼系統(tǒng)供應商 鄂州人工智能電銷機器人軟件 菏澤智能ai電銷機器人銷售公司 地圖標注字母的軟件 線上教育ai外呼系統(tǒng) 實用地圖標注app 400免費電話去哪申請 地圖標注商戶中心要收錢多少 css百度地圖標注位置顯示

學習java的正則表達式,抓取網(wǎng)頁并解析HTML部分內容  

package com.xiaofeng.picup;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** *//**
*
* @抓取頁面文章標題及內容(測試) 手動輸入網(wǎng)址抓取,可進一步自動抓取整個頁面的全部內容
*
*/
public class WebContent ...{
  /** *//**
   * 讀取一個網(wǎng)頁全部內容
   */
  public String getOneHtml(String htmlurl) throws IOException...{
    URL url;
    String temp;
    StringBuffer sb = new StringBuffer();
    try ...{
      url = new URL(htmlurl);
      BufferedReader in = new BufferedReader(new InputStreamReader(url
          .openStream(), "utf-8"));// 讀取網(wǎng)頁全部內容
      while ((temp = in.readLine()) != null) ...{
        sb.append(temp);
      }
      in.close();
    }catch(MalformedURLException me)...{
      System.out.println("你輸入的URL格式有問題!請仔細輸入");
      me.getMessage();
      throw me;
    }catch (IOException e) ...{
      e.printStackTrace();
      throw e;
    }
    return sb.toString();
  }
  /** *//**
   *
   * @param s
   * @return 獲得網(wǎng)頁標題
   */
  public String getTitle(String s) ...{
    String regex;
    String title = "";
    ListString> list = new ArrayListString>();
    regex = "title>.*?/title>";
    Pattern pa = Pattern.compile(regex, Pattern.CANON_EQ);
    Matcher ma = pa.matcher(s);
    while (ma.find()) ...{
      list.add(ma.group());
    }
    for (int i = 0; i  list.size(); i++) ...{
      title = title + list.get(i);
    }
    return outTag(title);
  }
  /** *//**
   *
   * @param s
   * @return 獲得鏈接
   */
  public ListString> getLink(String s) ...{
    String regex;
    ListString> list = new ArrayListString>();
    regex = "a[^>]*href=("([^"]*)"|'([^']*)'|([^s>]*))[^>]*>(.*?)/a>";
    Pattern pa = Pattern.compile(regex, Pattern.DOTALL);
    Matcher ma = pa.matcher(s);
    while (ma.find()) ...{
      list.add(ma.group());
    }
    return list;
  }
  /** *//**
   *
   * @param s
   * @return 獲得腳本代碼
   */
  public ListString> getScript(String s) ...{
    String regex;
    ListString> list = new ArrayListString>();
    regex = "script.*?/script>";
    Pattern pa = Pattern.compile(regex, Pattern.DOTALL);
    Matcher ma = pa.matcher(s);
    while (ma.find()) ...{
      list.add(ma.group());
    }
    return list;
  }
  /** *//**
   *
   * @param s
   * @return 獲得CSS
   */
  public ListString> getCSS(String s) ...{
    String regex;
    ListString> list = new ArrayListString>();
    regex = "style.*?/style>";
    Pattern pa = Pattern.compile(regex, Pattern.DOTALL);
    Matcher ma = pa.matcher(s);
    while (ma.find()) ...{
      list.add(ma.group());
    }
    return list;
  }
  /** *//**
   *
   * @param s
   * @return 去掉標記
   */
  public String outTag(String s) ...{
    return s.replaceAll(".*?>", "");
  }

您可能感興趣的文章:
  • JAVA使用爬蟲抓取網(wǎng)站網(wǎng)頁內容的方法
  • java抓取網(wǎng)頁數(shù)據(jù)獲取網(wǎng)頁中所有的鏈接實例分享
  • java正則表達式匹配網(wǎng)頁所有網(wǎng)址和鏈接文字的示例
  • java簡單網(wǎng)頁抓取的實現(xiàn)方法
  • Java中使用正則表達式獲取網(wǎng)頁中所有圖片的路徑
  • java 抓取網(wǎng)頁內容實現(xiàn)代碼
  • java抓取網(wǎng)頁數(shù)據(jù)示例
  • java實現(xiàn)網(wǎng)頁解析示例
  • 用javascrpt將指定網(wǎng)頁保存為Excel的代碼
  • Java獲取任意http網(wǎng)頁源代碼的方法

標簽:梅州 鞍山 恩施 池州 三亞 咸陽 六安 綿陽

巨人網(wǎng)絡通訊聲明:本文標題《Java用正則表達式如何讀取網(wǎng)頁內容》,本文關鍵詞  Java,用,正則,表達式,如何,;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Java用正則表達式如何讀取網(wǎng)頁內容》相關的同類信息!
  • 本頁收集關于Java用正則表達式如何讀取網(wǎng)頁內容的相關信息資訊供網(wǎng)民參考!
  • 推薦文章