Jsoup后台解析html、jsp网页
在一些网络爬虫或者从第三方网站抓取信息的程序都面临1个问题,如何从网页中把所需的信息提取出来,Jsoup是个比较好的选择,它能把网站内容解析成Document,再从document中取element就是个简单的事了。这里介绍1下Jsoup的基本用法。
首先需要下载jar包,jsoup-1.9.2.jar
1、Jsoup解析字符串
public void parseString()
{
String html = "<html><head><script type=\"text/javascript\">var date = new Date();alert(date);function sub(u){var token = document.getElementById(\"token\").value;var durl = u + \"token=\" + encodeURIComponent(token);window.open(durl);}</script></head><body><br/><br/>token:<input type=\"text\" name=\"token\" id=\"token\" style=\"width:500\" value=\"uGyUoJ8A6+ETMgIVYAHTpt/l/cY=\"/></input> <br/><br/><input class='butt' type=\"button\" value=\"打开本地\" name=\"sub\" onclick=\"sub('http://localhost:8080/mapbar-fieldwork/setting/setting!index.action?')\" style=\"left:200\"></input><input type=\"button\" value=\"打开44\" name=\"sub\" onclick=\"sub('http://10.30.20.44:8181/mapbar-fieldwork/setting/setting!index.action?')\" style=\"left:200\"></input><input type=\"button\" value=\"获取权限接口\" name=\"sub\" onclick=\"sub('http://localhost:8080/mapbar-fieldwork/newaccount/newaccount!queryUserPermissions.action?projectId=666&')\" style=\"left:200\"></input><input type=\"button\" value=\"获取权限接口\" name=\"sub\" onclick=\"sub('http://localhost:8080/mapbar-fieldwork/newaccount/newaccount!queryProjectPermissions.action?projectId=666&')\" style=\"left:200\"></input></body></html>"; //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);
}
Jsoup后台解析html、jsp网页的更多相关文章
- Jsoup库 解析DOM文档
DOM文档包括 HTML, XML等等 下载: http://jsoup.org/download Jsoup 获取数据的方式 //html 文本, url, 本地html String html = ...
- HttpClient+jsoup登录+解析 163邮箱
找了几个,只有这个靠谱,用的是httpclient4,另外还需要commons-lang和jsoup包 http://jsoup.org/ http://www.oschina.net/code/sn ...
- 【网络爬虫】【java】微博爬虫(四):数据处理——jsoup工具解析html、dom4j读写xml
之前提到过,对于简单的网页结构解析,可以直接通过观察法.手工写正则解析,可以做出来,比如网易微博.但是对于结构稍微复杂点的,比如新浪微博,如果还用正则,用眼睛一个个去找,未免太麻烦了. 本文介绍两个工 ...
- JSP网页处理过程
以下步骤表明了Web服务器是如何使用JSP来创建网页的: 就像其他普通的网页一样,您的浏览器发送一个HTTP请求给服务器. Web服务器识别出这是一个对JSP网页的请求,并且将该请求传递给JSP引擎 ...
- JSON字符串——后台解析系列
以前我们都是讲JSON字符串获取后,在前台进行展示.今天小编就交给大家后台解析展示数据的方法.非常方便,就以下代码: JObject obj = JObject.Parse(data); string ...
- JSP 网页格式判定执行哪一块html
JSP 网页格式判定执行哪一块html <!-- start --> <td height="166" colspan="3&q ...
- jsp网页连接mysql数据库
1.在mysql数据库中建立数据表 建一张测试表test,插入几条数据: 2.设置JDBC驱动 将 mysql-connector-java-5.0.8-bin.jar 拷到WebContent/WE ...
- 通过JSP网页连接MySQL数据库,从MySQL数据库中读出一张表并显示在JSP网页中
1.安装所需软件 ①安装java和tomcat,建立JSP网页最基础的软件②安装MySQL数据库(下载地址:https://www.mysql.com/)③安装Navicat Premium来查看数据 ...
- 在Eclipse中运行Web项目Jsp网页时提示端口被占用的解决办法:Several ports (8005, 8888, 8009) required by Tomcat v9.0 Server at localhost are already in use.
问题: 在Eclipse中运行Web项目Jsp网页启动Tomcat时提示端口被占用: Several ports (8005, 8080, 8009) required by Tomcat v9.0 ...
随机推荐
- CodeForces 616B Dinner with Emma
水题 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; +; ...
- cnn 文章
http://www.cnblogs.com/fengfenggirl/p/cnn_implement.html http://www.2cto.com/kf/201603/493553.html h ...
- NIPS 2016论文:英特尔中国研究院在神经网络压缩算法上的最新成果
NIPS 2016论文:英特尔中国研究院在神经网络压缩算法上的最新成果 http://www.leiphone.com/news/201609/OzDFhW8CX4YWt369.html 英特尔中国研 ...
- Struts2不配置result参数 进行跳转实现
的 this.getRequest().getRequestDispatcher("url").forward(this.getRequest(),this.getResponse ...
- Lua学习系列(五)
calling C functions from Lua 5.2 这篇文章也不错: http://blog.csdn.net/x356982611/article/details/26688287 h ...
- DNS架设准备+申请领域查询授权
1. 架设DNS服务器首先我们得安装一下的软件[root@bogon ~]# rpm -qa | grep ^bindbind-libs-9.8.2-0.37.rc1.el6.i686 <==给 ...
- shell之crontab
一 crontab高级用法 Crontab 主要是在做排程, 通常一般寫法大概都是如下: 0 0 * * * /usr/local/www/awstats/cgi-bin/awstats.sh 這種 ...
- VS2010 中 error 2732: 链接规范与的早期规范冲突 的解决
在实验室做项目的时候遇到了这个问题,终于整明白了. 一般来说这个错误出现在类似以下的语句中 extern "C" int yylex(void); extern "C&q ...
- Lua C Api lua_gettable 、lua_settable 、lua_next 使用详解
之前一直没理清lua_gettable和lua_settable的使用,今天理清了,顺便就做下笔记了.1.lua_gettable void lua_gettable (lua_State *L, i ...
- 数字(数学)操作类 Math Random 类 ,大数字操作类
Math 提供了大量的数学操作方法 Math类中所有的方法都是static 方法