0
当我尝试在我的Android应用中使用jsoup example时,像this这样的页面上,则找不到图像。这是因为内容是通过JavaScript加载的?Jsoup:查找指定页面上的所有图像以及子页面或内部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子页面(或图像链接)分析。
谢谢埃里克,但我忘了告诉你,我在Android应用使用jsoup,所以链接的JavaScript库,我不能使用。我尝试从webview中获取html作为字符串,但没有成功。 – Tomas
我一直在想什么,但我不能想出任何“简单”的东西。我会说:创建一个使用HtmlUnit(或其他库,如果它是另一个)的web服务,那么你从android使用该服务,你忘了Jsoup。我想不出别的什么:/。 –
埃里克,你是对的。我也是这么想的。但对我来说,现在在服务器端使用java应用程序的代价很高,所以也许在将来。但是,感谢您的回复和提示! – Tomas