在一些网络爬虫或者从第三方网站抓取信息的程序都面临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网页的更多相关文章

  1. Jsoup库 解析DOM文档

    DOM文档包括 HTML, XML等等 下载: http://jsoup.org/download Jsoup 获取数据的方式 //html 文本, url, 本地html String html = ...

  2. HttpClient+jsoup登录+解析 163邮箱

    找了几个,只有这个靠谱,用的是httpclient4,另外还需要commons-lang和jsoup包 http://jsoup.org/ http://www.oschina.net/code/sn ...

  3. 【网络爬虫】【java】微博爬虫(四):数据处理——jsoup工具解析html、dom4j读写xml

    之前提到过,对于简单的网页结构解析,可以直接通过观察法.手工写正则解析,可以做出来,比如网易微博.但是对于结构稍微复杂点的,比如新浪微博,如果还用正则,用眼睛一个个去找,未免太麻烦了. 本文介绍两个工 ...

  4. JSP网页处理过程

    以下步骤表明了Web服务器是如何使用JSP来创建网页的:  就像其他普通的网页一样,您的浏览器发送一个HTTP请求给服务器. Web服务器识别出这是一个对JSP网页的请求,并且将该请求传递给JSP引擎 ...

  5. JSON字符串——后台解析系列

    以前我们都是讲JSON字符串获取后,在前台进行展示.今天小编就交给大家后台解析展示数据的方法.非常方便,就以下代码: JObject obj = JObject.Parse(data); string ...

  6. JSP 网页格式判定执行哪一块html

    JSP 网页格式判定执行哪一块html <!-- start -->             <td height="166" colspan="3&q ...

  7. jsp网页连接mysql数据库

    1.在mysql数据库中建立数据表 建一张测试表test,插入几条数据: 2.设置JDBC驱动 将 mysql-connector-java-5.0.8-bin.jar 拷到WebContent/WE ...

  8. 通过JSP网页连接MySQL数据库,从MySQL数据库中读出一张表并显示在JSP网页中

    1.安装所需软件 ①安装java和tomcat,建立JSP网页最基础的软件②安装MySQL数据库(下载地址:https://www.mysql.com/)③安装Navicat Premium来查看数据 ...

  9. 在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 ...

随机推荐

  1. 超级好用的excel第三方组件

    http://www.cnblogs.com/MR_ke/archive/2010/02/25/1673243.html 前一篇介绍了怎么用com操作excel,后来有朋友说用NPOI好,下面我也来介 ...

  2. GB2312 编码

    GB2312编码范围:A1A1-FEFE,其中汉字编码范围:B0A1-F7FE. GB2312编码是第一个汉字编码国家标准,由中国国家标准总局1980年发布,1981年5月1日开始使用.GB2312编 ...

  3. mysql 二级索引

    mysql中每个表都有一个聚簇索引(clustered index ),除此之外的表上的每个非聚簇索引都是二级索引,又叫辅助索引(secondary indexes). 以InnoDB来说,每个Inn ...

  4. 51nod算法马拉松 contest7

    A题 链接:http://www.51nod.com/contest/problem.html#!problemId=1417 推荐链接:http://blog.csdn.net/a837199685 ...

  5. (中等) HDU 3265 Posters , 扫描线。

    Problem Description Ted has a new house with a huge window. In this big summer, Ted decides to decor ...

  6. el5,6,7的ntpdate服务

    在el5里没有ntpdate服务 在el6里有ntpdate服务 在el7里有ntpdate服务

  7. FIFO存储器

    FIFO( First In First Out)简单说就是指先进先出.由于微电子技术的飞速发展,新一代FIFO芯片容量越来越大,体积越来越小,价格越来越便宜.作为一种新型大规模集成电路,FIFO芯片 ...

  8. 【转】人工智能(AI)资料大全

    这里收集的是关于人工智能(AI)的教程.书籍.视频演讲和论文. 欢迎提供更多的信息. 在线教程 麻省理工学院人工智能视频教程 – 麻省理工人工智能课程 人工智能入门 – 人工智能基础学习.Peter ...

  9. 使用Cookie记住用户名和密码

    Login.jsp <form name = "f1" method="get" action="servlet/LoginServlet&qu ...

  10. sqlserver递归查询

    --递归查询 with cte as ( ' union all select k.id,k.Text, k.name,k.pid from Menu k inner join cte c on c. ...