mh m
mh m

Reputation: 75

find background image location in url content

i want to save the background image of "http://yooz.ir/" to my Disk by a java code. this image changes every few days. it has a download link blue arrow, but i don't find image location to save it by code. how i can find the location of this image in url content by jsoup, htmlunit or etc?

Upvotes: 1

Views: 214

Answers (2)

James W.
James W.

Reputation: 3055

I see this element is populated asynchronously so you need to use some webdriver automation tool, the most common is selenium

/*
import selenium, you can do it via maven;
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-server</artifactId>
    <version>2.45.0</version>
</dependency>
 */
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class SaveImageFromUrl {

    public static void main(String[] args) throws Exception {

        // download chrome driver http://chromedriver.storage.googleapis.com/index.html
        // or use firefox, w/e u like
        System.setProperty("webdriver.chrome.driver", chromeDriverLocation);

        WebDriver driver = new ChromeDriver(); // opens browsers
        driver.get("http://yooz.ir/"); // redirect to site
        // wait until element which contains the download link exist in page
        new WebDriverWait(driver, 5).until(new ExpectedCondition<WebElement>() {
            @Override
            public WebElement apply(WebDriver d) {
                return d.findElement(By.className("image-day__download"));
            }
        });

        // get the link inside the element via queryselector
        // https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector
        String img2download = driver.findElement(By.cssSelector(".image-day__download a")).getAttribute("href");
        System.out.println("img2download = " + img2download);

        //TODO download..

        driver.close();
    }

}

Upvotes: 1

Diego Martinoia
Diego Martinoia

Reputation: 4662

You have the wrong url. Here is the right one: http://imgs.yooz.ir/yooz/walls/yooz-950602-2.jpg

Upvotes: 1

Related Questions