首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在url内容中查找背景图像位置

在url内容中查找背景图像位置
EN

Stack Overflow用户
提问于 2016-08-25 21:52:36
回答 2查看 190关注 0票数 1

我想通过java代码将"http://yooz.ir/“的背景图像保存到我的磁盘中。这个图像每隔几天就会改变一次。它有一个下载链接blue arrow,但我找不到图像位置来通过代码保存它。我怎样才能找到这个图片在url内容中的位置,通过jsoup,htmlunit等?

EN

回答 2

Stack Overflow用户

发布于 2016-08-25 21:54:56

您输入了错误的url。下面是正确的一个:http://imgs.yooz.ir/yooz/walls/yooz-950602-2.jpg

票数 1
EN

Stack Overflow用户

发布于 2016-08-25 21:57:11

我看到这个元素填充了asynchronously,所以你需要使用一些webdriver自动化工具,最常见的是selenium

代码语言:javascript
复制
/*
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();
    }

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39147217

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档