从HTML文件解析图像的URL [英] parsing an image url from an html file

查看:147
本文介绍了从HTML文件解析图像的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过一个HTML文件进行搜索,然后让链接直接在该网页上的图像。此URL应被保存为一个字符串 - 多数民众赞成。问题是,我真的不知道如何下手。

I want to search through a html file and then get the url to an image on that page. This url should then be saved as a string - thats all. The problem is I really don t know how to start.

我当然知道应用程序的URL的网页,图像的位置。 作为一个例子让我们这个网址:

My app of course knows the url to the page where the image is located. As an example lets take this url:

在这个页面,我需要大的图像作为字符串的URL。当我查看源$ C ​​$ C我能找到的网址,但我不知道如何$ C c表示$ - 这是我需要的网址:

On this page I need the url of the big image as string. When I view the sourcecode I can locate the url but I dont know how to code that - this is the url I need:

(引号内的文字只)。

推荐答案

使用 JSoup 。这是一个HTML解析器,可以让你使用CSS选择器(如jQuery的)来访问DOM元素。

Use JSoup. It's a HTML parser that will allow you to access DOM elements using css selectors (like jQuery).

// 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
}

这篇关于从HTML文件解析图像的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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