濮阳杆衣贸易有限公司

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

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

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

學(xué)習(xí)java的正則表達(dá)式,抓取網(wǎng)頁并解析HTML部分內(nèi)容  

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;
/** *//**
*
* @抓取頁面文章標(biāo)題及內(nèi)容(測試) 手動輸入網(wǎng)址抓取,可進(jìn)一步自動抓取整個頁面的全部內(nèi)容
*
*/
public class WebContent ...{
  /** *//**
   * 讀取一個網(wǎng)頁全部內(nèi)容
   */
  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)頁全部內(nèi)容
      while ((temp = in.readLine()) != null) ...{
        sb.append(temp);
      }
      in.close();
    }catch(MalformedURLException me)...{
      System.out.println("你輸入的URL格式有問題!請仔細(xì)輸入");
      me.getMessage();
      throw me;
    }catch (IOException e) ...{
      e.printStackTrace();
      throw e;
    }
    return sb.toString();
  }
  /** *//**
   *
   * @param s
   * @return 獲得網(wǎng)頁標(biāo)題
   */
  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 去掉標(biāo)記
   */
  public String outTag(String s) ...{
    return s.replaceAll(".*?>", "");
  }

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

標(biāo)簽:梅州 鞍山 恩施 池州 三亞 咸陽 六安 綿陽

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Java用正則表達(dá)式如何讀取網(wǎng)頁內(nèi)容》,本文關(guān)鍵詞  Java,用,正則,表達(dá)式,如何,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Java用正則表達(dá)式如何讀取網(wǎng)頁內(nèi)容》相關(guān)的同類信息!
  • 本頁收集關(guān)于Java用正則表達(dá)式如何讀取網(wǎng)頁內(nèi)容的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    重庆市| 普格县| 通江县| 富裕县| 永胜县| 洪泽县| 边坝县| 泸定县| 长葛市| 分宜县| 利辛县| 衡阳市| 集安市| 汉寿县| 肃宁县| 徐闻县| 江永县| 通辽市| 永泰县| 平塘县| 兰西县| 小金县| 新乡市| 黄山市| 阿瓦提县| 民权县| 平远县| 建水县| 宁海县| 辰溪县| 班戈县| 如皋市| 巴南区| 塔城市| 探索| 德州市| 天祝| 静乐县| 英吉沙县| 文昌市| 绥阳县|