我想从网页下载一些图像,因为我正在编写一个爬虫。我为此页面测试了几个爬虫,但没有一个能按我的意愿工作。
第一步,我收集了 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