【问题标题】:Java: How to Scrape Images from Amazon with Selenium?Java:如何使用 Selenium 从亚马逊抓取图像?
【发布时间】:2015-03-22 01:36:03
【问题描述】:

我正在尝试使用 Selenium WebDriver 从亚马逊上的这个 URL 抓取页面左侧的 6 张图片:

http://www.amazon.com/EasyAcc%C2%AE-10000mAh-Brilliant-Smartphone-Bluetooth/dp/B00H9BEC8E

但是,无论我尝试什么都会导致错误。到目前为止我尝试过的:

  1. 我尝试直接使用 XPATH 抓取图像,然后使用“getAttributes”方法提取 src。例如,对于页面上的第一张图片,XPATH 是:

    .//*[@id='a-autoid-2']/span/input

所以我尝试了以下方法:

  String path1 = ".//*[@id='a-autoid-2']/span/input";
        String url = "http://www.amazon.com/EasyAcc%C2%AE-10000mAh-Brilliant-Smartphone-Bluetooth/dp/B00H9BEC8E";
        WebDriver driver = new FirefoxDriver();
        driver.get(url);
  WebElement s;
        s = driver.findElement(By.xpath(path1));
        String src;
        src = s.getAttribute("src");
        System.out.println(src);

但我找不到来源。

注意:仅当从某些类型的产品中抓取图像时才会出现此问题。例如,我可以使用 Selenium 轻松地从该产品中抓取图像:

http://www.amazon.com/Ultimate-Unification-Diet-Health-Disease/dp/0615797806/

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class mytest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub



        String path = ".//*[@id='imgThumbs']/div[2]/img";

        String url = "http://www.amazon.com/Ultimate-Unification-Diet-Health-Disease/dp/0615797806/";
        WebDriver driver = new FirefoxDriver();
        driver.get(url);


        WebElement s;
        s = driver.findElement(By.xpath(path));
        String src;
        src = s.getAttribute("src");
        System.out.println(src);

        driver.close();


    }
}

这段代码完美无缺。只有在抓取某些产品时似乎无法绕过它。

  1. 我尝试单击导致 iframe 打开的图像,但我也无法从此 iframe 中抓取图像,即使在切换到 iframe 之后也是如此:

    driver.switchTo().frame(IFRAMEID);

我知道我可以使用“截图”方法,但我想知道是否有办法直接抓取图像?

谢谢

【问题讨论】:

  • 我认为 XPath 有点复杂。为什么不使用 CSS 路径?
  • 我实际上也尝试过 CSS,但它不起作用。下面列出的解决方案有效。但是,我试图弄清楚为什么当我将鼠标悬停在图像上时 Firebug 没有显示这个 XPATH ..

标签: java selenium xpath


【解决方案1】:

试试这个代码

    String path = "//div[@id='imageBlock_feature_div']//span/img";

    String url = "http://rads.stackoverflow.com/amzn/click/0615797806";
    WebDriver driver = new FirefoxDriver();
    driver.get(url);

    List<WebElement> srcs;
    srcs = driver.findElements(By.xpath(path));

    for(WebElement src : srcs) {
        System.out.println(src.getAttribute("src"));
    }

    driver.close();

结果

2015-01-23 12:36:14 [main]-[INFO] Opened url: http://rads.stackoverflow.com/amzn/click/B00H9BEC8E
http://ecx.images-amazon.com/images/I/41cOP3mFX3L._SX38_SY50_CR,0,0,38,50_.jpg
http://ecx.images-amazon.com/images/I/51YkMhRXqcL._SX38_SY50_CR,0,0,38,50_.jpg
http://ecx.images-amazon.com/images/I/51nSbXF%2BCTL._SX38_SY50_CR,0,0,38,50_.jpg
http://ecx.images-amazon.com/images/I/31s%2B31F%2BQmL._SX38_SY50_CR,0,0,38,50_.jpg
http://ecx.images-amazon.com/images/I/41FmTOJEOOL._SX38_SY50_CR,0,0,38,50_.jpg
http://ecx.images-amazon.com/images/I/41U6qpLJ07L._SX38_SY50_CR,0,0,38,50_.jpg

但是,要获取 Amazon Images,我建议您尝试使用 Amazon API https://affiliate-program.amazon.com/gp/advertising/api/detail/main.html

好多了。

【讨论】:

  • 您的解决方案完美无缺。所以我想我的问题是我使用了错误的 XPATH?我使用 Firebug 在您的答案中找不到 XPATH。这是定制的吗?
猜你喜欢
  • 2019-12-07
  • 1970-01-01
  • 2017-03-11
  • 1970-01-01
  • 2022-11-13
  • 2014-05-08
  • 2010-12-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多