【发布时间】:2015-05-26 21:44:37
【问题描述】:
当我尝试在我的 Android 应用程序中的 this 等页面上使用 jsoup example 时,找不到任何图像。是因为内容是通过javascript加载的吗?
现在我的代码如下所示:
Document doc = Jsoup.connect(url).get();
Elements media = doc.select("[src]");
print("\nMedia: (%d)", media.size());
List<Link> tmpLinks = new ArrayList<>();
int i = 0;
for (Element src : media) {
if (src.tagName().equals("img")) {
if (!src.attr("abs:src").contains(".png") && !src.attr("abs:src").contains(".gif")) {
String widthString = src.attr("width");
int width;
if (!widthString.isEmpty()) {
width = Integer.parseInt(src.attr("width"));
}
else width = 0;
String heightString = src.attr("height");
int height;
if (!heightString.isEmpty()) {
height = Integer.parseInt(src.attr("height"));
}
else height = 0;
if (width == 0 || width >= minAllowedWidth) {
if (!src.attr("abs:src").isEmpty()) {
tmpLinks.add(new Link(i, src.attr("abs:src"), width, height));
}
}
i++;
print(" * %s: <%s> %sx%s (%s)",
src.tagName(), src.attr("abs:src"), src.attr("width"), src.attr("height"),
trim(src.attr("alt"), 20));
}
}
}
List<Link> noDuplicates = new ArrayList<>();
Set<String> titles = new HashSet<>();
for (Link link : tmpLinks ) {
if (titles.add(link.getUrl())) {
noDuplicates.add(link);
}
}
List<Link> finalLinks = new ArrayList<>();
for (Link link : noDuplicates) {
URL testUrl = new URL(link.getUrl());
URLConnection urlConnection = testUrl.openConnection();
urlConnection.connect();
int file_size = urlConnection.getContentLength();
System.out.println("Fetching size: " + link.getUrl() + " " + file_size);
if (file_size >= minAllowedFileSize) {
link.setSize(file_size);
finalLinks.add(link);
}
}
Collections.sort(finalLinks, new LinkSizeComparator());
第二个问题是,我可以用 this 之类的网站上的 jsoup 子页面(或图片链接)进行分析吗?
【问题讨论】: