【网络爬虫】使用jsoup对dom树解析
来源:互联网 发布:csol2控制台fps优化 编辑:程序博客网 时间:2024/05/29 03:13
基于jsoup的二次封装,对dom树解析。
package test;import java.util.LinkedList;import java.util.List;import junit.framework.TestCase;import com.vaolan.parser.JsoupHtmlParser;import com.vaolan.status.DataFormatStatus;import com.vaolan.utils.IOUtil;import com.vaolan.utils.StaticValue;public class RandomTest extends TestCase { public static void testCleanTest() { String fileName = "d:/test_2.txt"; String htmlSource = IOUtil.readFile(fileName, StaticValue.default_encoding); String cleanTxt = JsoupHtmlParser.getCleanTxt(htmlSource); System.out.println(cleanTxt); } public static void testgetTagCleanTxt() { String fileName = "d:/test_2.txt"; String htmlSource = IOUtil.readFile(fileName, StaticValue.default_encoding); String cleanTxt = JsoupHtmlParser.getTagContent(htmlSource, "inner", DataFormatStatus.CleanTxt); System.out.println(cleanTxt); } public static void testGetNestTagContent() { String fileName = "d:/test_2.txt"; String htmlSource = IOUtil.readFile(fileName, StaticValue.default_encoding); List<String> tagList = new LinkedList<String>(); tagList.add("outer"); // tagList.add("inner"); List<String> cleanTxt = JsoupHtmlParser.getNestTagContent(htmlSource, tagList, DataFormatStatus.CleanTxt, true); System.out.println(cleanTxt); } public static void testGetNodeContentBySelector() { String fileName = "d:/test_2.txt"; String htmlSource = IOUtil.readFile(fileName, StaticValue.default_encoding); List<String> selList = new LinkedList<String>(); selList.add("outer"); // selList.add(".subBrand"); selList.add("inner"); List<String> cleanTxt = JsoupHtmlParser.getNodeContentBySelector( htmlSource, selList, DataFormatStatus.TagAllContent, true); if (cleanTxt != null) { for (String item : cleanTxt) { System.out.println(item); } } else { System.out.println("结果集为null"); } } public static void removeSelector() { String fileName = "d:/test_2.txt"; String htmlSource = IOUtil.readFile(fileName, StaticValue.default_encoding); // List<String> list = JsoupHtmlParser.getAllHref(htmlSource); String selector = "outer"; List<String> removeList = new LinkedList<String>(); removeList.add("inner"); // removeList.add("span"); String result = JsoupHtmlParser.removeInnerContent(htmlSource, selector, removeList); // for (String url : list) { // System.out.println(url); // } System.out.println(result); } public static void main(String[] args) { // 得到html source的纯文本 // testCleanTest(); // testgetTagCleanTxt(); // testGetNestTagContent(); // testGetNodeContentBySelector(); removeSelector(); }}
测试结果:
0 0
- 【网络爬虫】使用jsoup对dom树解析
- Jsoup -- 网络爬虫解析器
- 【使用JSOUP实现网络爬虫】解析一个HTML字符串
- 【使用JSOUP实现网络爬虫】解析一个body片断
- 【使用JSOUP实现网络爬虫】解析一个HTML字符串
- 【使用JSOUP实现网络爬虫】解析一个body片断
- 【使用JSOUP实现网络爬虫】解析一个body片断
- 使用JSOUP实现网络爬虫
- 使用JSOUP实现网络爬虫
- 【使用JSOUP实现网络爬虫】使用DOM方法来遍历一个文档
- 【使用JSOUP实现网络爬虫】使用DOM方法来遍历一个文档
- 网络爬虫2----JSoup解析HTML
- Android Jsoup与HttpClient网络爬虫解析
- 使用Jsoup对豆瓣读书进行爬虫
- 【使用JSOUP实现网络爬虫】处理URLs
- 【使用JSOUP实现网络爬虫】处理URLs
- 【使用JSOUP实现网络爬虫】专栏
- 使用jsoup解析HTML之jsoup和HTML DOM简介
- LeetCode-282. Expression Add Operators (JAVA)表达式求值
- 函数传参中使用指针
- 复杂链表的复制
- 机器学习(八):寻找数据的分组——k均值聚类
- Hardcoded string “姓名”, should use @string resource警告
- 【网络爬虫】使用jsoup对dom树解析
- HDOJ 3507 Print Article (斜率优化DP)
- java缓存技术的介绍
- [数据结构]归并排序
- 献给青春的歌 · 致「 腾讯QQ 18 岁」
- HDOJ 1004 Let the Balloon Rise
- Matplotlib画图基础
- Codeforces803B Distances to Zero
- C语言中如何定义动态二维数组并输出