外国网站摘录的一个关于Jsoup的简单实例

news/2024/5/19 21:36:19/文章来源:https://blog.csdn.net/u011393661/article/details/44993101

jsoup HTML parser hello world examples

html parser

Jsoup, a HTML parser, its “jquery-like” and “regex” selector syntax is very easy to use and flexible enough to get whatever you want. Below are three examples to show you how to use Jsoup to get links, images, page title and “div” element content from a HTML page.

Download jsoup
The jsoup is available in Maven central repository. For non-Maven user, just download it from jsoup website.

  <dependency><groupId>org.jsoup</groupId><artifactId>jsoup</artifactId><version>1.7.1</version></dependency>

1. Grabs All Hyperlinks

This example shows you how to use jsoup to get page’s title and grabs all links from “google.com”.

HTMLParserExample1.java
package com.mkyong;import java.io.IOException;import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;public class HTMLParserExample1 {public static void main(String[] args) {Document doc;try {// need http protocoldoc = Jsoup.connect("http://google.com").get();// get page titleString title = doc.title();System.out.println("title : " + title);// get all linksElements links = doc.select("a[href]");for (Element link : links) {// get the value from href attributeSystem.out.println("\nlink : " + link.attr("href"));System.out.println("text : " + link.text());}} catch (IOException e) {e.printStackTrace();}}}

Output

title : Googlelink : http://www.google.com.my/imghp?hl=en&tab=wi
text : Imageslink : http://maps.google.com.my/maps?hl=en&tab=wl
text : Maps//omitted for readability
Note
It’s recommended to specify a “ userAgent” in Jsoup, to avoid HTTP 403 error messages.

    Document doc = Jsoup.connect("http://anyurl.com").userAgent("Mozilla").get();

2. Grabs All Images

The second example shows you how to use the Jsoup regex selector to grab all image files (png, jpg, gif) from “yahoo.com”.

HTMLParserExample2.java
package com.mkyong;import java.io.IOException;import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;public class HTMLParserExample2 {public static void main(String[] args) {Document doc;try {//get all imagesdoc = Jsoup.connect("http://yahoo.com").get();Elements images = doc2.select("img[src~=(?i)\\.(png|jpe?g|gif)]");for (Element image : images) {System.out.println("\nsrc : " + image.attr("src"));System.out.println("height : " + image.attr("height"));System.out.println("width : " + image.attr("width"));System.out.println("alt : " + image.attr("alt"));}} catch (IOException e) {e.printStackTrace();}}}

Output

src : http://l.yimg.com/a/i/mntl/ww/events/p.gif
height : 50
width : 202
alt : Yahoo!src : http://l.yimg.com/a/i/ww/met/intl_flag_icons/20111011/my_flag.gif
height : 
width : 
alt ://omitted for readability

3. Get Meta elements

The last example simulate an offline HTML page and use jsoup to parse the content. It grabs the “meta” keyword and description, and also the div element with the id of “color”.

HTMLParserExample3.java
package com.mkyong;import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;public class HTMLParserExample3 {public static void main(String[] args) {StringBuffer html = new StringBuffer();html.append("<!DOCTYPE html>");html.append("<html lang=\"en\">");html.append("<head>");html.append("<meta charset=\"UTF-8\" />");html.append("<title>Hollywood Life</title>");html.append("<meta name=\"description\" content=\"The latest entertainment news\" />");html.append("<meta name=\"keywords\" content=\"hollywood gossip, hollywood news\" />");html.append("</head>");html.append("<body>");html.append("<div id='color'>This is red</div> />");html.append("</body>");html.append("</html>");Document doc = Jsoup.parse(html.toString());//get meta description contentString description = doc.select("meta[name=description]").get(0).attr("content");System.out.println("Meta description : " + description);//get meta keyword contentString keywords = doc.select("meta[name=keywords]").first().attr("content");System.out.println("Meta keyword : " + keywords);String color1 = doc.getElementById("color").text();String color2 = doc.select("div#color").get(0).text();System.out.println(color1);System.out.println(color2);}}

Output

Meta description : The latest entertainment news
Meta keyword : hollywood gossip, hollywood news
This is red
This is red

4. Grabs Form Inputs

This code snippets shows you how to use Jsoup to grab HTML form inputs (name and value). For detail usage, please refer to this automate login a website with Java.

public void getFormParams(String html){Document doc = Jsoup.parse(html);//HTML form idElement loginform = doc.getElementById("your_form_id");Elements inputElements = loginform.getElementsByTag("input");List<String> paramList = new ArrayList<String>();for (Element inputElement : inputElements) {String key = inputElement.attr("name");String value = inputElement.attr("value");}}

5. Get Fav Icon

This code shows you how to use Jsoup to page’s favourite icon.

jSoupExample.java
package com.mkyong;import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;public class jSoupExample {public static void main(String[] args) {StringBuffer html = new StringBuffer();html.append("<html lang=\"en\">");html.append("<head>");html.append("<link rel=\"icon\" href=\"http://example.com/image.ico\" />");		//html.append("<meta content=\"/images/google_favicon_128.png\" itemprop=\"image\">");html.append("</head>");html.append("<body>");html.append("something");html.append("</body>");html.append("</html>");Document doc = Jsoup.parse(html.toString());String fav = "";Element element = doc.head().select("link[href~=.*\\.(ico|png)]").first();if(element==null){element = doc.head().select("meta[itemprop=image]").first();if(element!=null){fav = element.attr("content");}}else{fav = element.attr("href");}System.out.println(fav);}}

Output

http://example.com/image.ico

References

  • http://jsoup.org/
  • jsoup selector examples
  • jsoup to get page meta content



http://www.mkyong.com/java/jsoup-html-parser-hello-world-examples/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_865591.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Mac - MAMP - WordPress - 搭建 php+MySQL 网站开发环境

文章目录 下载MAMPWordpress 配置 下载 MAMP MAMP是Mac平台上一款用于Web开发的本地服务器环境的工具。 MAMP这几个首字母代表苹果的OSX系统上的Macintosh、Apache、MySQL和PHP&#xff0c;即 MAMP 内含 Apache 服务器、PHP 安装套件以及MySQL安装套件。 https://www.mamp.i…

推荐几个设计师常去的优秀素材网站!收藏好哦!

1、创客贴 网址&#xff1a;www.chuangkit.com 创客贴是一个专业的平面设计工具网站&#xff0c;功能强大、全面&#xff0c;操作十分简单&#xff0c;有超过10w的各种场景模板&#xff0c;支持在线多人、多终端协作&#xff0c;关键还免费&#xff01; 2、包图网 网址&#x…

作为一名平面设计师,你必须知道的一些素材网站

随着互联网日新月异的发展速度&#xff0c;平面设计成为了各个行业宣传中一个重要部分。作为一名优秀平面设计师&#xff0c;不管是学习还是工作当中&#xff0c;都少不了需要一些灵感&#xff0c;但是常常为找不到灵感而苦恼&#xff0c;在你灵感枯竭&#xff0c;创意干涸的时…

设计师不可错过的2022年设计类网站大搜集,素材 教程 案例 全都有

经常看大家找素材着急&#xff01;为没有灵感发疯&#xff01;特意为大家整理2017最新的一些关于设计的素材网站、自学网站&#xff0c;字体网站、等一些压箱底的干货包含全部链接&#xff01; 希望能给大家带来帮助。喜欢可以加关注。一起分享设计资源&#xff01; 花瓣网 ht…

某漫画网站MD5参数分析

原创 Mr.Yang 菜鸟童靴 今天 声明:本文只作学习研究,禁止用于非法用途,否则后果自负,如有侵权,请告知删除,谢谢! 练习网站平台: aHR0cHMlM0EvL3R3Lm1hbmh1YWd1aS5jb20v 抓取目标: 抓到对应的漫画,图片地址 开始我们的分析: 根据图上,标注可以很容易看到这里有…

【转载】如何在网站中添加音乐

来源&#xff1a;http://www.ido321.com/1042.html 发现有很多的个人博客中添加了背景音乐&#xff0c;以增强用户体验。LZ搜集到了两种在网站中添加音乐的方式。 一、豆瓣的FM 这个非常简单&#xff0c;一段代码就可以实现。 <p><iframe name"iframe_canvas&quo…

使用JSSDK实现网站的QQ登录

使用JSSDK实现网站的QQ登录 进入QQ互联官网&#xff1a;https://connect.qq.com/index.html 进行开发者注册并审核认证【实名认证】&#xff1a;首先使用QQ账号登录上述的QQ互联官网&#xff1b;接着填写开发者审核认证资料&#xff0c;需提交&#xff1a;开发者类型&#xf…

网站流量统计

常使用web服务器的朋友大都了解&#xff0c;一般的web server有两部分日志&#xff1a; 一是运行中的日志&#xff0c;它主要记录运行的一些信息&#xff0c;尤其是一些异常错误日志信息 二是访问日志信息&#xff0c;它记录的访问的时间&#xff0c;IP&#xff0c;访问的…

利用网页压缩来提升网站浏览速度

网站的访问速度是由多个因素所共同决定的&#xff0c;这些因素例如应用程序的响应速度、网络带宽、服务器性能、与客户端之间的网络传输速度等等。其中最重要的 一个因素是应用程序本身的响应速度&#xff0c;因此当你为网站性能所苦恼时&#xff0c;你第一个需要着手进行处理的…

baidu网址提交|baidu收录网址提交|向百度提交网站

向百度提交网站:http://www.baidu.com/search/url_submit.html 方便百度爬行你的网站&#xff0c;收录你的信息 -------------------------- 新闻&#xff1a; Google天使投资人透露投资策略&#xff1a;团队第一 网站导航: 博客园首页 新闻 .NET频道 社区 博问 闪存…

大型网站技术架构

架构演变第一步&#xff1a;物理分离webserver和数据库 最开始&#xff0c;由于某些想法&#xff0c;于是在互联网上搭建了一个网站&#xff0c;这个时候甚至有可能主机都是租借的&#xff0c;但由于这篇文章我们只关注架构的演变历程&#xff0c;因此就假设这个时候已经是托管…

如何设计通用的网站模板

http://www.serverjia.cn/html/wlpz/c/yy/2009/0311/29412.html 现在网络上已经到处可以看到使用模板开发出来的网站。使用模板开发网站有很多好处&#xff0c;最主要的就是模板与程序完全脱离&#xff0c;用户可以根据规定好的标签任意开发模板&#xff0c;导入到模板引擎里就…

opc-ua技术资料网站汇总

OPC官方网址&#xff1a;https://opcfoundation.org/ OPC中国官网&#xff1a; http://www.chinaopc.org/ ----------------------------------------------------------------- http://www.advosol.com/ 一个收费的国外OPC开发包 ------------------------------------…

大型网站架构技能图谱(Java版)

在大型网站技术架构中&#xff0c;涉及到许多的技术&#xff0c;这些技术是具备大型网站架构设计能力的前提和基础。因为笔者对Java比较熟悉&#xff0c;所以下面的技术图谱都是围绕Java后端工程师展开的。如有不正确或者不完善的地方&#xff0c;欢迎补充。

Fortran在线学习网站

Fortran Tutorialhttps://www.tutorialspoint.com/fortran/index.htm

8款HTML5/jQuery应用助网站走向高上大

在WEB3.0的时代&#xff0c;我们的网站不仅要实现实用价值&#xff0c;更要为用户设计优秀的用户体验。jQuery是一个不错的JS框架&#xff0c;结合目前最新的HTML5技术&#xff0c;我们可以将自己的网站脱胎换骨&#xff0c;立马走向高上大&#xff0c;至少在前端页面上。 1、…

localhost:6666/无法访问此网站

在写demo的时候遇到这个问题 反反复复检查了代码&#xff0c;操作了好几次&#xff0c;还是不行&#xff0c;后来查询到&#xff0c;是一些浏览器对某些端口做了限制&#xff0c;比如谷歌浏览器 更改了端口之后重新跑代码&#xff0c;没问题

自学宝典:10个学习Android开发的网站推荐

发表于 2015-02-23 10:22| 18829次阅读| 来源 简书| 90 条评论| 作者 Tikitoo 移动开发 Android 开发者 GitHub CodePath android开发 摘要&#xff1a;本文作者Tikitoo总结自己在学习Android开发过程中发现的好网站&#xff0c;包括Android Developers、GitHub等&#xff0c;以…

12个免费学习编程的网站

发表于 2015-09-09 17:15| 7774次阅读| 来源 Entrepreneur| 16 条评论| 作者 John Rampton 编程 程序员 摘要&#xff1a;无论想学什么,也无论出于什么目的想学,都需要你保持好奇心。但是作为刚想入门的新手&#xff0c;面对眼前海量的信息&#xff0c;或许根本不知道从哪里开始…

从12306.cn谈大网站架构与性能优化

原文地址&#xff1a;http://www.ha97.com/5169.html PS&#xff1a;关于12306.cn网站&#xff0c;前些时间&#xff0c;骂的人很多&#xff0c;但是这网站的压力和架构不是一般非专业人生想得这么简单。下文是资深架构师陈皓写的关于12306.cn购票网站的架构和性能系列分析&…