0

我想从网页下载一些图像,因为我正在编写一个爬虫。我为此页面测试了几个爬虫,但没有一个能按我的意愿工作。

第一步,我收集了 770+ 个相机型号的链接(parent_url),然后我想在每个链接中收集图像(child_urls)。但是,该页面的组织方式与child_urls返回的 html 相同parent_url

这是我收集相机链接的代码:

public List<String> html_compiler(String url, String exp, String atr){
    List<String> outs = new ArrayList<String>(); 
    try {
        Document doc = Jsoup.connect(url).get();

        Elements links = doc.select(exp);
        for (Element link : links) {
            outs.add(link.attr(atr));
            System.out.println("\nlink : " + link.attr(atr));
        }
    } catch (IOException | SelectorParseException e) {
        e.printStackTrace();
    }
    return outs;
}

使用此代码,我收集链接

String expCam = "tr[class='gallery cameras'] > td[class='title'] > a[href]";
String url = "https://www.dpreview.com/sample-galleries?category=cameras";
String atr = "href";
List<String> cams = html_compiler(url, exp, atr); // This gives me the links of individual cameras

String exp2 = "some expression";
html_compiler(cams.get(0), exp2, "src"); // --> this should give me image links of the first
                                         //camera but webpage returns same html as above

我怎么解决这个问题?我很想听听根据相机型号对图像进行分类的其他页面。(除了 Flickr)

编辑: 例如在 java 中,以下两个链接给出了相同的 html。

https://www.dpreview.com/sample-galleries?category=cameras

https://www.dpreview.com/sample-galleries/2653563139/nikon-d1-review-samples-one

4

1 回答 1

1

要了解如何获取图像链接,了解页面在浏览器中的加载方式非常重要。如果您单击画廊链接,将触发一个 javascript 事件处理程序。创建的图像查看器然后从数据服务器加载图像。图片链接是通过 javascript 请求的,因此仅通过解析 html 是不可见的。图片链接的请求 URL 是https://www.dpreview.com/sample-galleries/data/get-gallery获取画廊中的图片,您必须添加画廊 id。画廊 ID 由href画廊链接的属性提供。链接看起来像https://www.dpreview.com/sample-galleries/2653563139/nikon-d1-review-samples-one。在这种情况下2653563139是画廊 id。获取上面给出的链接并将画廊 ID 添加?galleryId=2653563139到 URL 的末尾,以获取包含创建画廊所需的所有数据的 json 对象。查找url字段中的images数组来获取您的图像。

总结一下:

您从href属性中获得的链接:https ://www.dpreview.com/sample-galleries/2653563139/nikon-d1-review-samples-one

画廊编号:2653563139

请求网址:https ://www.dpreview.com/sample-gallery/data/get-gallery

您需要的 json 对象:https ://www.dpreview.com/sample-galleries/data/get-gallery?galleryId=2653563139

您在 json 对象中查找的 url:"url":"https://3.img-dpreview.com/files/p/TS1800x1200~sample_galleries/2653563139/7864344228.jpg"

最后是你的图片链接:https ://3.img-dpreview.com/files/p/TS1800x1200~sample_galleries/2653563139/7864344228.jpg

如果您想进一步解释,请发表评论。

于 2016-08-16T15:12:47.493 回答