博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Jsoup后台解析html、jsp网页
阅读量:6955 次
发布时间:2019-06-27

本文共 1287 字,大约阅读时间需要 4 分钟。

在一些网络爬虫或者从第三方网站抓取信息的程序都面临1个问题,如何从网页中把所需的信息提取出来,Jsoup是个比较好的选择,它能把网站内容解析成Document,再从document中取element就是个简单的事了。这里介绍1下Jsoup的基本用法。

首先需要下载jar包,

1、Jsoup解析字符串

public void parseString()	{		String html = "
token:
"; //Jsoup解析html Document doc =Jsoup.parse(html,"utf-8"); //根据id获取元素 Element e1 = doc.getElementById("token"); //根据属性获取元素s Elements e2s = doc.getElementsByAttribute("onclick"); //根据属性+属性值 Elements e3s = doc.getElementsByAttributeValue("type", "text"); //根据class Elements e4s = doc.getElementsByClass("butt"); //根据 标签 Elements e5s = doc.getElementsByTag("head"); Elements e6s = doc.select("input[type]"); p(e6s); }

2、Jsoup解析url

Jsoup可以直接解析1个网址,把网站的返回内容解析出来

public void parseUrl()	{		try 		{			URL url = new URL("http://www.baidu.com");			Document doc = Jsoup.parse(url, 1000);			Elements e1s = doc.select("a[href=http://news.baidu.com]");			p(e1s);		} catch (IOException e) 		{			e.printStackTrace();		}	}

3、Jsoup解析本地文件

可以把html文件解析出来

public void parseFile()	{		File file = new File("C:/Users/Administrator/Desktop/测试页面.html");		try {			Document doc = Jsoup.parse(file, "GBK");			p(doc);		} catch (IOException e) {			e.printStackTrace();		}	}

public static void p(Object o)	{		System.out.println(o);	}

转载于:https://www.cnblogs.com/chenjack/p/6298214.html

你可能感兴趣的文章
XGBoost:在Python中使用XGBoost
查看>>
python基础知识~ 序列化
查看>>
Activity的启动模式(android:launchMode)
查看>>
java设计模式演示样例
查看>>
创建与删除索引
查看>>
HTML5新增核心工具——canvas
查看>>
改动file header (測)
查看>>
微软职位内部推荐-Senior Speech TTS
查看>>
UVA - 10574 Counting Rectangles
查看>>
HDU3336-Count the string(KMP)
查看>>
常用API接口签名验证参考
查看>>
Linux中find常见用法示例
查看>>
bootstrap 模态框动态加载数据
查看>>
初始化构造函数中定义的实体集合,方便嵌套类型的遍历
查看>>
深入理解css3中nth-child和 nth-of-type的区别
查看>>
MySQL慢查询Explain Plan分析
查看>>
MyBatis原理分析之三:初始化(配置文件读取和解析)
查看>>
180321
查看>>
Spark2.1.0之源码分析——事件总线
查看>>
Htmlparser专题
查看>>