- 2024-02-15 15:27:52
- 5524 热度
- 0 评论
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
参考官网:https://jsoup.org/
0 评论
留下评论
热门标签
- Spring(403)
- Boot(208)
- Spring Boot(187)
- Java(82)
- Cloud(82)
- Spring Cloud(82)
- Security(60)
- Spring Security(54)
- Boot2(51)
- Spring Boot2(51)
- Redis(31)
- SQL(29)
- Mysql(25)
- IDE(24)
- Dalston(24)
- JDBC(22)
- IDEA(22)
- mongoDB(22)
- MVC(22)
- Web(21)
- CLI(20)
- Alibaba(19)
- SpringMVC(19)
- SpringBoot(17)
- Docker(17)
- Git(16)
- Eclipse(16)
- Vue(16)
- ORA(15)
- JPA(15)
- Apache(15)
- Mybatis(14)
- Oracle(14)
- jdk(14)
- Tomcat(14)
- Linux(14)
- HTTP(14)
- XML(13)
- JdbcTemplate(13)
- OAuth(13)
- Nacos(13)
- Pro(13)
- Data(12)
- JSON(12)
- OAuth2(12)
- stream(11)
- int(11)
- Myeclipse(11)
- not(10)
- Bug(10)
- maven(9)
- Map(9)
- Hystrix(9)
- ast(9)
- APP(8)
- Bit(8)
- API(8)
- session(8)
- Window(8)
- Swagger(8)
- Github(7)
- JavaMail(7)
- Cache(7)
- File(7)
- mail(7)
- IntelliJ(7)
- windows(7)
- too(7)
- HTML(7)
- RabbitMQ(6)
- star(6)
- and(6)
- Excel(6)
- Log4J(6)
- pushlet(6)
- apt(6)
- read(6)
- Freemarker(6)
- WebFlux(6)
- JSP(6)
- Bean(6)
- error(6)
- nginx(6)
- Server(6)
- ueditor(6)
- jar(6)
- ehcache(6)
- UDP(6)
- rdquo(5)
- PHP(5)
- Struts(5)
- string(5)
- Syntaxhighlighter(5)
- script(5)
- Tool(5)
- Controller(5)
- swagger2(5)
- ldquo(5)
- input(5)
- Servlet(5)