在网址内容中找到背景图片位置

时间:2016-08-25 13:52:36

标签: java

我想保存" http://yooz.ir/"的背景​​图片通过java代码到我的磁盘。此图像每隔几天就会更改一次。它有一个下载链接blue arrow,但我找不到图像位置以通过代码保存它。如何通过jsoup,htmlunit等在url内容中找到此图像的位置?

2 个答案:

答案 0 :(得分:1)

你的网址错误。这是正确的:http://imgs.yooz.ir/yooz/walls/yooz-950602-2.jpg

答案 1 :(得分:1)

我看到此元素已填充异步  所以你需要使用一些webdriver自动化工具,最常见的是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();
    }

}