Jsoup 和附件列表
Jsoup and list of attachments
我有这个 HTML 块:
ul class="list_attachments"><li>
<a href="www.site1.com"><img src='pdf.png' alt='pdf'/> File1</a></li><li>
<a href="www.site2.com"><img src='pdf.png' alt='pdf'/> File2</a></li>
</ul>
我想提取所有 "a href" 行,特别是站点和名称文件信息。
所以我尝试了这个:
String [] fileName = new String[2];
String [] url = new String[2];
int i=0;
attachments = document.select(".list_attachments");
for (Element attachment : attachments) {
String fileName[i] = attachment.text();
String url[i] = attachment.select("a").attr("href");
i++;
}
但结果是:
String fileName = "File1 File2";
String url = "www.site1.com";
问题是只有一个附件元素,而不是我预期的两个。
如何解决这个问题?谢谢
.select(".list_attachments")
只能 select <ul class="list_attachments">
所以它 returns Elements
只有一个 <ul>
元素。我怀疑您想要 select .list_attachments
中存在的所有 <a ...>
个元素,然后获取它们的 text
和 href
。在那种情况下,您的代码应该看起来更像
Elements anchors = document.select(".list_attachments a");
for (Element anchor : anchors) {
fileName[i] = anchor.text();
url[i] = anchor.attr("href");
i++;
}
我有这个 HTML 块:
ul class="list_attachments"><li>
<a href="www.site1.com"><img src='pdf.png' alt='pdf'/> File1</a></li><li>
<a href="www.site2.com"><img src='pdf.png' alt='pdf'/> File2</a></li>
</ul>
我想提取所有 "a href" 行,特别是站点和名称文件信息。 所以我尝试了这个:
String [] fileName = new String[2];
String [] url = new String[2];
int i=0;
attachments = document.select(".list_attachments");
for (Element attachment : attachments) {
String fileName[i] = attachment.text();
String url[i] = attachment.select("a").attr("href");
i++;
}
但结果是:
String fileName = "File1 File2";
String url = "www.site1.com";
问题是只有一个附件元素,而不是我预期的两个。 如何解决这个问题?谢谢
.select(".list_attachments")
只能 select <ul class="list_attachments">
所以它 returns Elements
只有一个 <ul>
元素。我怀疑您想要 select .list_attachments
中存在的所有 <a ...>
个元素,然后获取它们的 text
和 href
。在那种情况下,您的代码应该看起来更像
Elements anchors = document.select(".list_attachments a");
for (Element anchor : anchors) {
fileName[i] = anchor.text();
url[i] = anchor.attr("href");
i++;
}