从 html 文件解析图像 url

RCK*_*K69 1 html java parsing android image

我想搜索 html 文件,然后获取该页面上图像的 url。然后这个 url 应该被保存为一个字符串——仅此而已。问题是我真的不知道如何开始。

我的应用程序当然知道图像所在页面的 url。我们以这个 url 为例:


在此页面上,我需要大图像的 url 作为字符串。当我查看源代码时,我可以找到 url,但我不知道如何编码 - 这是我需要的 url:


(仅限引号内的文本)。

npe*_*npe 5

使用JSoup。它是一个 HTML 解析器,允许您使用 css 选择器(如 jQuery)访问 DOM 元素。

// Parse your HTML:
// 1. From string:
Document doc = JSoup.parse(htmlAsString);

// 2. Or from an URL:
Document doc = JSoup.connect("http://my.awesome.site.com/").get();

// Then select images inside it:
Elements images = doc.select("img");

// Then iterate
for (Element el : images) {
    String imageUrl = el.attr("src");

    // TODO: Do something with the URL
}
Run Code Online (Sandbox Code Playgroud)