我想搜索一个 html 文件,然后获取该页面上图像的 url。这个 url 然后应该保存为一个字符串 - 仅此而已。问题是我真的不知道如何开始。
我的应用程序当然知道图像所在页面的 url。 作为一个例子,让我们使用这个 url:
在这个页面上,我需要大图像的 url 作为字符串。当我查看源代码时,我可以找到 url,但我不知道如何编码 - 这是我需要的 url:
(仅引号内的文字)。
最佳答案
使用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
}
关于java - 从 html 文件解析图像 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11010171/