網路爬蟲-去除網頁原始碼中的標籤
像百度百科、維基百科獲取到的網頁原始碼經常會含有HTML標籤,要想獲取到跟頁面上我們看到的一樣的內容,就需要對網頁原始碼進行處理。
下面是處理網頁原始碼中的HTML標籤的工具類:
public class StringUtil {
public static String stripHTML(String html) {
String noHTMLString = "";
html = html.replaceAll("&", "&");
Matcher m = Pattern
.compile("&#(\\d+);", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL | Pattern.CANON_EQ)
.matcher(html);
boolean b = false;
int i = 0;
while (m.find()) {
if (i > 500) {
System.out.println(i);
}
i++;
html = html.replace("&#" + m.group(1) + ";", (char) Integer.parseInt(m.group(1)) + "");
b = true;
}
if (!b) {
m = Pattern
.compile("&#x([\\da-f]+);",
Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL | Pattern.CANON_EQ)
.matcher(html);
int j = 0;
while (m.find()) {
if (j > 500) {
System.out.println(j);
}
j++;
html = html.replaceAll("&#[x|X]" + m.group(1) + ";", (char) Integer.parseInt(m.group(1), 16) + "");
}
}
noHTMLString = html.replaceAll("<\\s*(?:br|Br|BR|bR|div|DIV|Div|p|P|td|TD|Td)\\s*(?:[^>])*\\s*>", "\n")
.replaceAll(" ", " ").replaceAll("", " ").replaceAll(" ", " ").replaceAll("\\<.*?\\>", "")
.replaceAll("&(?:g|l)t", "");
return noHTMLString.trim();
}
}
處理後就可以得到跟我們網頁上看到的一樣的內容了,有時候有些特殊的字元可能沒有被處理掉,可以通過.replaceAll()替換掉即可。
相關文章
- 《網頁爬蟲》網頁爬蟲
- Python網路爬蟲之爬取淘寶網頁頁面 MOOC可以執行的程式碼Python爬蟲網頁
- 網路爬蟲爬蟲
- a標籤去除原始樣式
- 爬蟲——網頁爬取方法和網頁解析方法爬蟲網頁
- 網路爬蟲有什麼用?怎麼爬?手把手教你爬網頁(Python程式碼)爬蟲網頁Python
- 網路爬蟲的原理爬蟲
- python爬蟲---網頁爬蟲,圖片爬蟲,文章爬蟲,Python爬蟲爬取新聞網站新聞Python爬蟲網頁網站
- 網路爬蟲——爬蟲實戰(一)爬蟲
- 網路爬蟲示例爬蟲
- 網路爬蟲精要爬蟲
- 什麼是Python網路爬蟲?常見的網路爬蟲有哪些?Python爬蟲
- 網路爬蟲的反扒策略爬蟲
- 手把手教你利用爬蟲爬網頁(Python程式碼)爬蟲網頁Python
- node:爬蟲爬取網頁圖片爬蟲網頁
- 網頁爬蟲--未完成網頁爬蟲
- python 爬蟲網頁登陸Python爬蟲網頁
- python網路爬蟲應用_python網路爬蟲應用實戰Python爬蟲
- python爬蟲爬取網頁中文亂碼問題的解決Python爬蟲網頁
- python網路爬蟲_Python爬蟲:30個小時搞定Python網路爬蟲視訊教程Python爬蟲
- 爬蟲(6) - 網頁資料解析(2) | BeautifulSoup4在爬蟲中的使用爬蟲網頁
- python DHT網路爬蟲Python爬蟲
- 網路爬蟲專案爬蟲
- 爬蟲抓取網頁的詳細流程爬蟲網頁
- [Python] 網路爬蟲與資訊提取(1) 網路爬蟲之規則Python爬蟲
- 《Python3網路爬蟲開發實戰》PDF+原始碼+《精通Python爬蟲框架Scrapy》中英文PDF原始碼...Python爬蟲原始碼框架
- [Python3網路爬蟲開發實戰] 2-爬蟲基礎 2-網頁基礎Python爬蟲網頁
- 【爬蟲】網頁抓包工具--Fiddler爬蟲網頁
- 爬蟲抓取網頁資料原理爬蟲網頁
- Html網頁中meta標籤及用法詳解HTML網頁
- [網路爬蟲] 網路爬蟲實踐:大麥網演唱會預約搶票 【待續】爬蟲
- 匿名IP在網路爬蟲中的應用探索爬蟲
- 網路爬蟲在商業分析中的應用爬蟲
- java去除xml檔案中的標籤JavaXML
- python網路爬蟲(14)使用Scrapy搭建爬蟲框架Python爬蟲框架
- 網路爬蟲(python專案)爬蟲Python
- 什麼是網路爬蟲爬蟲
- 網路爬蟲大型教程(二)爬蟲
- 專案--python網路爬蟲Python爬蟲