【Java爬虫】爬取网页中的内容,提取其中文字
生活随笔
收集整理的這篇文章主要介紹了
【Java爬虫】爬取网页中的内容,提取其中文字
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
挺亂的,臨時存一下
package cn.hanquan.craw;import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.regex.Pattern;public class HtmlText {public static String SRC = "FirstPage.html";public static void main(String[] args) throws IOException {// 爬取網頁寫進txtInputStream is2 = UrlCrawBoke.doGet("https://blog.csdn.net/sinat_42483341/article/details/95988975");String pageStr = UrlCrawBoke.inputStreamToString(is2, "UTF-8");is2.close();FileWriter is1 = new FileWriter("before.txt");is1.write(pageStr); // 可以用 write("666")is1.close();// 正則提取:從html中提取純文本String after = Html2Text(pageStr.toString());FileWriter is = new FileWriter("after.txt");is.write(after); // 可以用 write("666")is.close();System.out.println(after);}// 從html中提取純文本public static String Html2Text(String inputString) {String htmlStr = inputString; // 含html標簽的字符串String textStr = "";java.util.regex.Pattern p_script;java.util.regex.Matcher m_script;java.util.regex.Pattern p_style;java.util.regex.Matcher m_style;java.util.regex.Pattern p_html;java.util.regex.Matcher m_html;try {String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; // 定義script的正則表達式{或<script[^>]*?>[\\s\\S]*?<\\/script>String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; // 定義style的正則表達式{或<style[^>]*?>[\\s\\S]*?<\\/style>String regEx_html = "<[^>]+>"; // 定義HTML標簽的正則表達式p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);m_script = p_script.matcher(htmlStr);htmlStr = m_script.replaceAll(""); // 過濾script標簽p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);m_style = p_style.matcher(htmlStr);htmlStr = m_style.replaceAll(""); // 過濾style標簽p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);m_html = p_html.matcher(htmlStr);htmlStr = m_html.replaceAll(""); // 過濾html標簽textStr = htmlStr;} catch (Exception e) {System.err.println("Html2Text: " + e.getMessage());}// 剔除空格行textStr = textStr.replaceAll("[ ]+", "");textStr = textStr.replaceAll("1", "").replaceAll("2", "").replaceAll("3", "").replaceAll("4", "").replaceAll("5", "").replaceAll("6", "").replaceAll("7", "").replaceAll("8", "").replaceAll("9", "").replaceAll("0", "");textStr = textStr.replaceAll("(?m)^\\s*$(\\n|\\r\\n)", "");textStr = textStr.replaceAll("\t", "");textStr = textStr.replaceAll(" ", "").replace(">", "").replace("—", "");// 還有什么 查一查textStr = textStr.replaceAll("\\\\", "");// 正則表達式中匹配一個反斜杠要用四個反斜杠textStr = textStr.replaceAll("\r\n", "");textStr = textStr.replaceAll("\n", "");return textStr;// 返回文本字符串} }class UrlCrawBoke {public static InputStream doGet(String urlstr) throws IOException {URL url = new URL(urlstr);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36");InputStream inputStream = conn.getInputStream();return inputStream;}public static String inputStreamToString(InputStream is, String charset) throws IOException {byte[] bytes = new byte[1024];int byteLength = 0;StringBuffer sb = new StringBuffer();while ((byteLength = is.read(bytes)) != -1) {sb.append(new String(bytes, 0, byteLength, charset));}return sb.toString();} }總結
以上是生活随笔為你收集整理的【Java爬虫】爬取网页中的内容,提取其中文字的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【PAT甲级 素数判断 进制转换】101
- 下一篇: 【Java文件操作(七)】序列化:将自定