Jsoup:如何获取图像的绝对URL? [英] Jsoup: how to get an image's absolute url?

查看:83
本文介绍了Jsoup:如何获取图像的绝对URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jsoup中是否有一种方法可以提取图像的绝对URL,就像可以获取链接的绝对URL一样?

Is there a way in jsoup to extract an image absolute url, much like one can get a link's absolute url?

考虑以下在http://www.example.com/

<img src="images/chicken.jpg" width="60px" height="80px">

我想得到http://www.example.com/images/chicken.jpg.我该怎么办?

I would like to get http://www.example.com/images/chicken.jpg. What should I do?

推荐答案

一旦有了图像元素,例如:

Once you have the image element, e.g.:

Element image = document.select("img").first();
String url = image.absUrl("src");
// url = http://www.example.com/images/chicken.jpg

或者:

String url = image.attr("abs:src");

Jsoup具有内置的 absUrl() 使用所有节点的基本URL(可能与从文档中检索到的URL不同)将属性解析为绝对URL的方法.

Jsoup has a builtin absUrl() method on all nodes to resolve an attribute to an absolute URL, using the base URL of the node (which could be different from the URL the document was retrieved from).

另请参见使用URL jsoup文档.

See also the Working with URLs jsoup documentation.

这篇关于Jsoup:如何获取图像的绝对URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆