国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Regex > 正文

Java用正則表達式如何讀取網頁內容

2020-03-16 21:02:17
字體:
來源:轉載
供稿:網友

java用正則表達式讀取網頁內容,通過抓取文章標題及內容,進一步專區整個頁面的全部內容,本文代碼簡單易懂,對java用正則表達式讀取網頁內容感興趣的朋友可以參考下

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

 

 
  1. package com.xiaofeng.picup; 
  2. import java.io.BufferedReader; 
  3. import java.io.IOException; 
  4. import java.io.InputStreamReader; 
  5. import java.net.MalformedURLException; 
  6. import java.net.URL; 
  7. import java.util.ArrayList; 
  8. import java.util.HashMap; 
  9. import java.util.List; 
  10. import java.util.regex.Matcher; 
  11. import java.util.regex.Pattern; 
  12. /** *//** 
  13. * 
  14. * @抓取頁面文章標題及內容(測試) 手動輸入網址抓取,可進一步自動抓取整個頁面的全部內容 
  15. * 
  16. */ 
  17. public class WebContent ...{ 
  18. /** *//** 
  19. * 讀取一個網頁全部內容 
  20. */ 
  21. public String getOneHtml(String htmlurl) throws IOException...{ 
  22. URL url; 
  23. String temp; 
  24. StringBuffer sb = new StringBuffer(); 
  25. try ...{ 
  26. url = new URL(htmlurl); 
  27. BufferedReader in = new BufferedReader(new InputStreamReader(url 
  28. .openStream(), "utf-8"));// 讀取網頁全部內容 
  29. while ((temp = in.readLine()) != null) ...{ 
  30. sb.append(temp); 
  31. in.close(); 
  32. }catch(MalformedURLException me)...{ 
  33. System.out.println("你輸入的URL格式有問題!請仔細輸入"); 
  34. me.getMessage(); 
  35. throw me; 
  36. }catch (IOException e) ...{ 
  37. e.printStackTrace(); 
  38. throw e; 
  39. return sb.toString(); 
  40. /** *//** 
  41. * 
  42. * @param s 
  43. * @return 獲得網頁標題 
  44. */ 
  45. public String getTitle(String s) ...{ 
  46. String regex; 
  47. String title = ""
  48. List<String> list = new ArrayList<String>(); 
  49. regex = "<title>.*?</title>"
  50. Pattern pa = Pattern.compile(regex, Pattern.CANON_EQ); 
  51. Matcher ma = pa.matcher(s); 
  52. while (ma.find()) ...{ 
  53. list.add(ma.group()); 
  54. for (int i = 0; i < list.size(); i++) ...{ 
  55. title = title + list.get(i); 
  56. return outTag(title); 
  57. /** *//** 
  58. * 
  59. * @param s 
  60. * @return 獲得鏈接 
  61. */ 
  62. public List<String> getLink(String s) ...{ 
  63. String regex; 
  64. List<String> list = new ArrayList<String>(); 
  65. regex = "<a[^>]*href=("([^"]*)"|'([^']*)'|([^s>]*))[^>]*>(.*?)</a>"; 
  66. Pattern pa = Pattern.compile(regex, Pattern.DOTALL); 
  67. Matcher ma = pa.matcher(s); 
  68. while (ma.find()) ...{ 
  69. list.add(ma.group()); 
  70. return list; 
  71. /** *//** 
  72. * 
  73. * @param s 
  74. * @return 獲得腳本代碼 
  75. */ 
  76. public List<String> getScript(String s) ...{ 
  77. String regex; 
  78. List<String> list = new ArrayList<String>(); 
  79. regex = "<script.*?</script>"
  80. Pattern pa = Pattern.compile(regex, Pattern.DOTALL); 
  81. Matcher ma = pa.matcher(s); 
  82. while (ma.find()) ...{ 
  83. list.add(ma.group()); 
  84. return list; 
  85. /** *//** 
  86. * 
  87. * @param s 
  88. * @return 獲得CSS 
  89. */ 
  90. public List<String> getCSS(String s) ...{ 
  91. String regex; 
  92. List<String> list = new ArrayList<String>(); 
  93. regex = "<style.*?</style>"
  94. Pattern pa = Pattern.compile(regex, Pattern.DOTALL); 
  95. Matcher ma = pa.matcher(s); 
  96. while (ma.find()) ...{ 
  97. list.add(ma.group()); 
  98. return list; 
  99. /** *//** 
  100. * 
  101. * @param s 
  102. * @return 去掉標記 
  103. */ 
  104. public String outTag(String s) ...{ 
  105. return s.replaceAll("<.*?>"""); 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 绥滨县| 祁阳县| 仪陇县| 屏山县| 乐至县| 雅江县| 福安市| 百色市| 木里| 黄浦区| 集安市| 柳林县| 北流市| 浠水县| 荆州市| 丹棱县| 公安县| 湟源县| 夏邑县| 阿勒泰市| 南丰县| 肇东市| 西安市| 平安县| 江孜县| 太保市| 呼玛县| 莲花县| 宾阳县| 湘乡市| 慈溪市| 太保市| 揭阳市| 乌兰县| 达孜县| 抚远县| 屯留县| 睢宁县| 明星| 黔东| 日喀则市|