Java獲取任意http網(wǎng)頁源代碼的方法
本文實例講述了JAVA獲取任意http網(wǎng)頁源代碼。分享給大家供大家參考,具體如下:
JAVA獲取任意http網(wǎng)頁源代碼可實現(xiàn)如下功能:
1. 獲取任意http網(wǎng)頁的代碼
2. 獲取任意http網(wǎng)頁去掉HTML標(biāo)簽的代碼
Webpage類:
/**
* 網(wǎng)頁操作相關(guān)類
*/
package test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author winddack
*
*/
public class Webpage {
private String pageUrl;//定義需要操作的網(wǎng)頁地址
private String pageEncode="UTF8";//定義需要操作的網(wǎng)頁的編碼
public String getPageUrl() {
return pageUrl;
}
public void setPageUrl(String pageUrl) {
this.pageUrl = pageUrl;
}
public String getPageEncode() {
return pageEncode;
}
public void setPageEncode(String pageEncode) {
this.pageEncode = pageEncode;
}
//定義取源碼的方法
public String getPageSource()
{
StringBuffer sb = new StringBuffer();
try {
//構(gòu)建一URL對象
URL url = new URL(pageUrl);
//使用openStream得到一輸入流并由此構(gòu)造一個BufferedReader對象
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), pageEncode));
String line;
//讀取www資源
while ((line = in.readLine()) != null)
{
sb.append(line);
}
in.close();
}
catch (Exception ex)
{
System.err.println(ex);
}
return sb.toString();
}
//定義一個把HTML標(biāo)簽刪除過的源碼的方法
public String getPageSourceWithoutHtml()
{
final String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定義script的正則表達(dá)式
final String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定義style的正則表達(dá)式
final String regEx_html = "<[^>]+>"; // 定義HTML標(biāo)簽的正則表達(dá)式
final String regEx_space = "\\s*|\t|\r|\n";//定義空格回車換行符
String htmlStr = getPageSource();//獲取未處理過的源碼
Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
Matcher m_script = p_script.matcher(htmlStr);
htmlStr = m_script.replaceAll(""); // 過濾script標(biāo)簽
Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
Matcher m_style = p_style.matcher(htmlStr);
htmlStr = m_style.replaceAll(""); // 過濾style標(biāo)簽
Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
Matcher m_html = p_html.matcher(htmlStr);
htmlStr = m_html.replaceAll(""); // 過濾html標(biāo)簽
Pattern p_space = Pattern.compile(regEx_space, Pattern.CASE_INSENSITIVE);
Matcher m_space = p_space.matcher(htmlStr);
htmlStr = m_space.replaceAll(""); // 過濾空格回車標(biāo)簽
htmlStr = htmlStr.trim(); // 返回文本字符串
htmlStr = htmlStr.replaceAll(" ", "");
htmlStr = htmlStr.substring(0, htmlStr.indexOf("。")+1);
return htmlStr;
}
}
調(diào)用:
Webpage page=new Webpage();
page.setPageUrl("http://www.baidu.com");
String code=page.getPageSourceWithoutHtml();
System.out.println(code);
PS:這里再為大家提供2款非常方便的正則表達(dá)式工具供大家參考使用:
JavaScript正則表達(dá)式在線測試工具:
http://tools.jb51.net/regex/javascript
正則表達(dá)式在線生成工具:
http://tools.jb51.net/regex/create_reg
更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java正則表達(dá)式技巧大全》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計有所幫助。
- 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用正則表達(dá)式如何讀取網(wǎng)頁內(nèi)容
- java實現(xiàn)網(wǎng)頁解析示例
- 用javascrpt將指定網(wǎng)頁保存為Excel的代碼
相關(guān)文章
JAVA加密算法- 非對稱加密算法(DH,RSA)的詳細(xì)介紹
這篇文章主要介紹了JAVA加密算法- 非對稱加密算法(DH,RSA),詳細(xì)介紹了DH,RSA的用法和示例,需要的朋友可以了解一下。2016-11-11
Dependency ‘XXX:‘ not found問題的三步解決
這篇文章主要介紹了Dependency ‘XXX:‘ not found問題的三步解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
SpringBoot中使用Quartz管理定時任務(wù)的方法
這篇文章主要介紹了SpringBoot中使用Quartz管理定時任務(wù)的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
Java 確保某個Bean類被最后執(zhí)行的幾種實現(xiàn)方式
這篇文章主要介紹了Java 確保某個BeanDefinitionRegistryPostProcessor Bean被最后執(zhí)行的幾種實現(xiàn)方式,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下2021-03-03
基于Springboot+Junit+Mockito做單元測試的示例
本篇文章主要介紹了基于Springboot+Junit+Mockito做單元測試的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02

