Java小强个人技术博客站点    手机版
当前位置: 首页 >> 开源 >> Jsoup加载HTML的三种方式

Jsoup加载HTML的三种方式

108641 开源 | 2016-6-13

Jsoup加载HTML的三种方式,上一篇说的只是一种方式,直接从HTTP源网站获取。


从字符串解析

来自用户输入,一个文件或一个网站的HTML字符串,你可能需要对它进行解析并取其内容,或校验其格式是否完整,或想修改它。

String html = "<html><head><title>First parse</title></head>"
  + "<body><p>from www.javacui.com</p></body></html>";
Document doc = Jsoup.parse(html);

只要解析的不是空字符串,就能返回一个结构合理的文档,其中包含(至少) 一个head和一个body元素。

一旦拥有了一个Document,你就可以使用Document中适当的方法或它父类 Element和Node中的方法来取得相关数据。

实用示例:

package com.cui.test;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
/**
 * 从字符串解析HTML
 * @author java小强
 */
public class StringHtmlSpider {
	public static void main(String[] args) {
		try {
			String html = "<html><head><title>First parse</title></head>"
					+ "<body><p>from www.javacui.com</p></body></html>";
			Document doc = Jsoup.parse(html);
			Elements elements = doc.getElementsByTag("p");// 根据标签获取
			Element e = elements.get(0);// 因为我知道只有一个p
			System.out.println(e.text());
			// 打印 from www.javacui.com
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


从本地文件加载

在本机硬盘上有一个HTML文件,需要对它进行解析从中抽取数据或进行修改。本示例HTML文件内容和上面示例字符串内容一致。

File input = new File("D:\\javacui.html");
Document doc = Jsoup.parse(input, "UTF-8");

这个方法用来加载和解析一个HTML文件。如在加载文件的时候发生错误,将抛出IOException,应作适当处理。

实用示例:

package com.cui.test;
import java.io.File;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
/**
 * 从本地文件解析HTML
 * @author java小强
 */
public class LocalDiskSpider {
	public static void main(String[] args) {
		try {
			File input = new File("D:\\javacui.html");
			Document doc = Jsoup.parse(input, "UTF-8");
			Elements elements = doc.getElementsByTag("p");// 根据标签获取
			Element e = elements.get(0);// 因为我知道只有一个p
			System.out.println(e.text());
			// 打印 from www.javacui.com
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


来自网络

你需要从一个网站获取和解析一个HTML文档,并查找其中的相关数据。

Document doc = Jsoup.connect("http://www.javacui.com/").get();
String title = doc.title();

connect(String url) 方法创建一个新的 Connection, 和 get() 取得和解析一个HTML文件。如果从该URL获取HTML时发生错误,便会抛出 IOException,应适当处理。

Connection 接口还提供一个方法链来解决特殊请求,具体如下:

Document doc = Jsoup.connect("http://example.com")
  .data("query", "Java")
  .userAgent("Mozilla")
  .cookie("auth", "token")
  .timeout(3000)
  .post();

这个方法只支持Web URLs (http和https 协议)。

使用代码参考:http://www.javacui.com/opensource/463.html 


Jsoup加载HTML的三种方式


参考官网:https://jsoup.org/ 

推荐您阅读更多有关于“ 爬虫 jsoup html解析 网络抓取 ”的文章

上一篇:mysql参数max_allowed_packet 下一篇:jsoup入门示例程序(网络爬虫)

猜你喜欢

发表评论:

评论:

回复 Java小强 评论于 2021-04-27 08:22
jsoup is a Java library for working with real-world HTML. It provides a very convenient API for fetching URLs and extracting and manipulating data, using the best of HTML5 DOM methods and CSS selectors.
jsoup implements the WHATWG HTML5 specification, and parses HTML to the same DOM as modern browsers do.
scrape and parse HTML from a URL, file, or string
find and extract data, using DOM traversal or CSS selectors
manipulate the HTML elements, attributes, and text
clean user-submitted content against a safe white-list, to prevent XSS attacks
output tidy HTML
jsoup is designed to deal with all varieties of HTML found in the wild; from pristine and validating, to invalid tag-soup; jsoup will create a sensible parse tree.

jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址、HTML文本内容。它提供了一套非常省力的API,可通过DOM,CSS以及类似于jQuery的操作方法来取出和操作数据。
jsoup实现tml5规范,并将HTML解析为与现代浏览器相同的DOM
1)从一个URL,文件或字符串中解析HTML
2)使用DOM或CSS选择器来查找、取出数据
3)可操作HTML元素、属性、文本
注意:jsoup是基于MIT协议发布的,可放心使用于商业项目。