Jsoup获取所有链接

从一个URL获得一个页面。然后提取页面中的所有链接、图片和其它辅助内容。并检查URLs和文本信息。

运行下面程序需要指定一个URLs作为参数

package org.jsoup.examples;import org.jsoup.Jsoup;
import org.jsoup.helper.Validate;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;import java.io.IOException;/*** Example program to list links from a URL.*/
public class ListLinks {public static void main(String[] args) throws IOException {Validate.isTrue(args.length == 1, "usage: supply url to fetch");String url = args[0];print("Fetching %s...", url);Document doc = Jsoup.connect(url).get();Elements links = doc.select("a[href]");Elements media = doc.select("[src]");Elements imports = doc.select("link[href]");print("\nMedia: (%d)", media.size());for (Element src : media) {if (src.tagName().equals("img"))print(" * %s: <%s> %sx%s (%s)",src.tagName(), src.attr("abs:src"), src.attr("width"), src.attr("height"),trim(src.attr("alt"), 20));elseprint(" * %s: <%s>", src.tagName(), src.attr("abs:src"));}print("\nImports: (%d)", imports.size());for (Element link : imports) {print(" * %s <%s> (%s)", link.tagName(),link.attr("abs:href"), link.attr("rel"));}print("\nLinks: (%d)", links.size());for (Element link : links) {print(" * a: <%s>  (%s)", link.attr("abs:href"), trim(link.text(), 35));}}private static void print(String msg, Object... args) {System.out.println(String.format(msg, args));}private static String trim(String s, int width) {if (s.length() > width)return s.substring(0, width-1) + ".";elsereturn s;}
}
org/jsoup/examples/ListLinks.java

输出结果

Fetching http://news.ycombinator.com/...Media: (38)* img: <http://ycombinator.com/images/y18.gif> 18x18 ()* img: <http://ycombinator.com/images/s.gif> 10x1 ()* img: <http://ycombinator.com/images/grayarrow.gif> x ()* img: <http://ycombinator.com/images/s.gif> 0x10 ()* script: <http://www.co2stats.com/propres.php?s=1138>* img: <http://ycombinator.com/images/s.gif> 15x1 ()* img: <http://ycombinator.com/images/hnsearch.png> x ()* img: <http://ycombinator.com/images/s.gif> 25x1 ()* img: <http://mixpanel.com/site_media/images/mixpanel_partner_logo_borderless.gif> x (Analytics by Mixpan.)Imports: (2)* link <http://ycombinator.com/news.css> (stylesheet)* link <http://ycombinator.com/favicon.ico> (shortcut icon)Links: (141)* a: <http://ycombinator.com>  ()* a: <http://news.ycombinator.com/news>  (Hacker News)* a: <http://news.ycombinator.com/newest>  (new)* a: <http://news.ycombinator.com/newcomments>  (comments)* a: <http://news.ycombinator.com/leaders>  (leaders)* a: <http://news.ycombinator.com/jobs>  (jobs)* a: <http://news.ycombinator.com/submit>  (submit)* a: <http://news.ycombinator.com/x?fnid=JKhQjfU7gW>  (login)* a: <http://news.ycombinator.com/vote?for=1094578&dir=up&whence=%6e%65%77%73>  ()* a: <http://www.readwriteweb.com/archives/facebook_gets_faster_debuts_homegrown_php_compiler.php?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+readwriteweb+%28ReadWriteWeb%29&utm_content=Twitter>  (Facebook speeds up PHP)* a: <http://news.ycombinator.com/user?id=mcxx>  (mcxx)* a: <http://news.ycombinator.com/item?id=1094578>  (9 comments)* a: <http://news.ycombinator.com/vote?for=1094649&dir=up&whence=%6e%65%77%73>  ()* a: <http://groups.google.com/group/django-developers/msg/a65fbbc8effcd914>  ("Tough. Django produces XHTML.")* a: <http://news.ycombinator.com/user?id=andybak>  (andybak)* a: <http://news.ycombinator.com/item?id=1094649>  (3 comments)* a: <http://news.ycombinator.com/vote?for=1093927&dir=up&whence=%6e%65%77%73>  ()* a: <http://news.ycombinator.com/x?fnid=p2sdPLE7Ce>  (More)* a: <http://news.ycombinator.com/lists>  (Lists)* a: <http://news.ycombinator.com/rss>  (RSS)* a: <http://ycombinator.com/bookmarklet.html>  (Bookmarklet)* a: <http://ycombinator.com/newsguidelines.html>  (Guidelines)* a: <http://ycombinator.com/newsfaq.html>  (FAQ)* a: <http://ycombinator.com/newsnews.html>  (News News)* a: <http://news.ycombinator.com/item?id=363>  (Feature Requests)* a: <http://ycombinator.com>  (Y Combinator)* a: <http://ycombinator.com/w2010.html>  (Apply)* a: <http://ycombinator.com/lib.html>  (Library)* a: <http://www.webmynd.com/html/hackernews.html>  ()* a: <http://mixpanel.com/?from=yc>  ()

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

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

相关文章

【OpenCV 例程200篇】215. 基于多段线绘制近似椭圆

OpenCV 例程200篇 总目录 【youcans 的 OpenCV 例程200篇】215. 基于多段线绘制近似椭圆 如果需要对椭圆渲染进行更多控制&#xff0c;或者绘制粗略的椭圆边界&#xff0c;可以使用 cv.ellipse2Poly 检索曲线&#xff0c;然后使用多段线进行渲染或使用fillPoly进行填充。 函数…

【OpenCV 例程200篇】216. 绘制多段线和多边形

专栏地址&#xff1a;『youcans 的 OpenCV 例程200篇』 文章目录&#xff1a;『youcans 的 OpenCV 例程200篇-总目录』 【youcans 的 OpenCV 例程200篇】216. 绘制多段线和多边形 函数 cv.polylines() 用来绘制多边形曲线或多段线。 函数 cv.fillPoly() 用来绘制一个或多个填充…

Jsoup设置属性值

在处理html时&#xff0c;我们有时候可能想在解析一个Document之后修改其中的某些属性值&#xff0c;如图片地址、class名称等各种属性&#xff0c;然后再保存到磁盘或都输出到前台页面。 可以使用属性设置方法 Element.attr(String key, String value), 和 Elements.attr(St…

matlab mat文件 太大,MATLAB .mat文件中的开销过大

这对我来说似乎是个错误.解决方法是以块的形式写入预分配的数组.从预先分配开始&#xff1a;fid fopen(01_hit12.par, r);data fread(fid, inf, uint8);nlines nnz(data 10) 1;fclose(fid);matObj.moleculeNumber zeros(1,nlines,uint8);matObj.isotopeologueNumber zer…

Jsoup设置一个元素的HTML内容

获取一个元素中的HTML内容&#xff0c;可以使用Element中的HTML设置方法。 示例 Element div doc.select("div").first(); // <div></div> div.html("<p>lorem ipsum</p>"); // <div><p>lorem ipsum</p><…

【youcans 的图像处理学习课】总目录

专栏地址&#xff1a;『youcans 的图像处理学习课』 【youcans 的图像处理学习课】总目录 youcansqq.com, 【youcans 的图像处理学习课】 更新日期&#xff1a;2022-06-30 本系列是学习冈萨雷斯《数字图像处理&#xff08;第四版&#xff09;》的笔记&#xff0c;篇目也是对照…

mysql数据库连接异常,mysql连接数据库时处理连接异常

连接数据库时处理连接异常 $db new PDO(‘mysql:hostlocalhost;dbnametest’, $user, $pass);$rs $db->query(“SELECT aa,bb,cc FROM foo”);if ($db->errorCode() ! ’00000′){print_r($db->errorInfo());exit;}$arr $rs->fetchAll();print_r($arr);$db nul…

【OpenCV 例程200篇】217. 鼠标交互获取多边形区域(ROI)

OpenCV 例程200篇 总目录 【youcans 的 OpenCV 例程200篇】217. 鼠标交互获取多边形区域 函数 cv.selectROI 可以通过鼠标在图像上选择感兴趣的矩形区域&#xff08;ROI&#xff0c;region of interest&#xff09;。 如果要通过鼠标在图像上选择感兴趣的多边形区域&#xff0c…

Jsoup设置元素的文本内容

修改一个HTML文档中的文本内容。 示例如下 Element div doc.select("div").first(); // <div></div> div.text("five > four"); // <div>five > four</div> div.prepend("First "); div.append(" Last&qu…

php+分针和时针重合,时钟问题—两针重合

​ 时钟问题—两针重合含义&#xff1a;钟面上的分针追上时针与之重合。这种追击&#xff0c;总是分针追时针&#xff0c;追击速度为分针每分钟前进的6度减去时针每分钟前进的0.5度&#xff0c;等于5.5度。由于钟面是圆形&#xff0c;追击分为分针在后和在前两种情况&#x…

【OpenCV 例程200篇】218. 多行倾斜文字水印

OpenCV 例程200篇 总目录 【youcans 的 OpenCV 例程200篇】218. 多行倾斜文字水印 8.1 添加多行倾斜文字水印 水印是叠加在图像上的标志、签名、文本或图案&#xff0c;用于保护图像的版权。 先在黑色背景上添加图像或文字制作水印&#xff0c;再使用 cv.addWeight 函数&…

Jsoup消除不受信任的HTML(用于防止XSS的攻击)

在开发网站的时候&#xff0c;经常会提供用户评论的功能。有些不怀好意的用户&#xff0c;会搞一些脚本到评论内容中&#xff0c;而这些脚本可能会破坏整个页面的行为&#xff0c;更严重的是获取一些机要信息&#xff0c;此时需要清理该HTML&#xff0c;以避免跨站脚本cross-si…

【OpenCV 例程200篇】219. 添加数字水印(盲水印)

OpenCV 例程200篇 总目录 【youcans 的 OpenCV 例程200篇】219. 添加数字水印&#xff08;盲水印&#xff09; 8.2 添加数字盲水印 数字水印&#xff0c;是指将特征信息嵌入音频、图像或是视频等数字信号中。 数字水印分为明水印和盲水印&#xff08;blind watermark&#x…

搜索栏联想词提示

在我们需要游览一些网站的时候&#xff0c;我们经常需要使用搜索引擎来进行搜索&#xff0c;无论是百度谷歌还是搜狐等&#xff0c;我们都需要在搜索栏中输入相关的搜索词&#xff0c;当我们点击进行输入的时候&#xff0c;下面会给出许许多多的提示词&#xff0c;程序自动联想…

【OpenCV 例程200篇】220.对图像进行马赛克处理

文章目录&#xff1a;『youcans 的 OpenCV 例程200篇 - 总目录』 【youcans 的 OpenCV 例程200篇】220.对图像进行马赛克处理 9. 图像的马赛克处理 马赛克效果是广泛使用的图像和视频处理方法。将图像中指定区域的色阶细节劣化&#xff0c;造成色块模糊的效果&#xff0c;看上…

主成分分析得分 matlab,主成分分析Matlab程序设计

PCA步骤&#xff1a;(1)对原始数据进行标准化处理(2)计算样本相关系数矩阵(3)计算相关系数矩阵R的特征值和相应的特征向量(4)选择重要的主成分&#xff0c;写出主成分表达式案例问题&#xff1a;企业综合实力排序企业综合实力评价表&#xff1a;企业序号净利润率/%固定资产利润…

【OpenCV 例程200篇】223. 特征提取之多边形拟合(cv.approxPolyDP)

『youcans 的 OpenCV 例程200篇 - 总目录』 【youcans 的 OpenCV 例程200篇】223. 特征提取之多边形拟合 目标特征的基本概念 通过图像分割获得多个区域&#xff0c;得到区域内的像素集合或区域边界像素集合。我们把感兴趣的人或物称为目标&#xff0c;目标所处的区域就是目标…

matlab指令vpa(j10),matlab中vpa函数

Matlab中矩阵函数_IT/计算机_专业资料。Matlab中矩阵函数 矩阵转置...Matlab 中 solve 函数主要是用来求解线性方程组的解析解或者精确解。对于得 出的结果是符号变量,可以通过 vpa()得出任意位数的数值解! solve 函数的语法定义主要有......控制系统matlab常用函数的使用_计算…

Eclipse快捷键生成语句

当我们使用Eclipse进行项目的开发的时候&#xff0c;有的时候我们需要重复输入main方法和输出语句等&#xff0c;下面就教给大家如何在Eclipse开发中使用快捷键生长main方法和输出语句。 快捷键生成语句 生成main方法&#xff1a;输入"main" &#xff0c;按ALT/&…

【OpenCV 例程200篇】221.加密马赛克图像处理与解密复原

『youcans 的 OpenCV 例程200篇 - 总目录』 【youcans 的 OpenCV 例程200篇】221.加密马赛克图像处理与解密复原 9. 图像的马赛克处理 马赛克效果是广泛使用的图像和视频处理方法。将图像中指定区域的色阶细节劣化&#xff0c;造成色块模糊的效果&#xff0c;看上去像是一个个…