jsoup API
jsoup API
- 1.解析一個簡單的html文件
- 1.1 Jsoup 干凈的解析結(jié)果
1.解析一個簡單的html文件
/*** @ClassName: JouspT1* @Description: TODO Jsoup解析* @author jmwang.erics* @date 2021年3月17日**/ public class JouspT1 {/*** @Title: main* @Description: TODO 解析和遍歷一個HTML文檔* @param @param args 參數(shù)* @return void 返回類型* @throws*/public static void main(String[] args) { // String htmlT = importHTMLFile(null); // System.out.println(htmlT);String html = "<html><head><title id='www'>First parse</title><title class='1'>First2 parse2</title></head>" + "<body><p>Parsed HTML into a doc.</p></body></html>";//返回html格式/*** 走一個Parser.parse(html, "");方法* 使用的是TreeBuilder生成樹類的parse(html, baseUri, ParseErrorList.noTracking());解析創(chuàng)建樹,最后返回給Document*/Document parse = Jsoup.parse(html);System.out.println(parse);//通過 id獲取Element elementById = parse.getElementById("www");System.out.println(elementById);//通過 tag獲取Elements allElements = elementById.getElementsByTag("title");System.out.println(allElements);//通過class獲取Elements elementsByClass = parse.getElementsByClass("1");System.out.println(elementsByClass);}/*** * @Title: generalAnalysisHTML* @Description: TODO 導(dǎo)入HTML文件* @param @param path* @param @return 參數(shù)* @return String 返回類型* @throws*/public static String importHTMLFile(String path) {File file = new File(path);FileReader fileReader = null;BufferedReader bufferedReader = null;StringBuilder html = new StringBuilder();try {fileReader = new FileReader(file);bufferedReader = new BufferedReader(fileReader);String tempString = null;while ((tempString=bufferedReader.readLine())!=null) {html.append(tempString);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return html.toString();} }1.1 Jsoup 干凈的解析結(jié)果
直接解決
總結(jié)
- 上一篇: aix网口配置IP
- 下一篇: 揭秘跨越普通权限的神秘地带