parsing an image url from an html file

后端 未结 3 1544
我在风中等你
我在风中等你 2021-01-16 04:49

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

3条回答
  •  粉色の甜心
    2021-01-16 05:39

    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
    }
    

提交回复
热议问题