Java 选择并迭代具有相同名称的元素和子元素(Jsoup)
我需要通过jsoup解析一个页面。该页面具有标记为Java 选择并迭代具有相同名称的元素和子元素(Jsoup),java,html,jsoup,Java,Html,Jsoup,我需要通过jsoup解析一个页面。该页面具有标记为div、h3、a等的元素。我想对这些元素进行解析,然后选择a(即标题)以显示在jList中 例如,该页面如下所示: <div class="start"> <div class="g"> <div class="abc"> <a class="picture" href="www.img.com"><img src="img" alt="imag
div
、h3
、a
等的元素。我想对这些元素进行解析,然后选择a
(即标题)以显示在jList
中
例如,该页面如下所示:
<div class="start">
<div class="g">
<div class="abc">
<a class="picture" href="www.img.com"><img src="img" alt="image1"></a>
<div class="xyz">
<h3 class="_r">
<a class="title" href="www.example.com" onmousedown="return rwt(this,'','','','1','adf','','ahahh','','',event)">THIS IS <em>example</em>1</a>
</h3>
</div>
</div>
</div>
<div class="g">
<div class="abc">
<a class="picture" href="www.img.com"><img src="img" alt="image2"></a>
<div class="xyz">
<h3 class="_r">
<a class="title" href="www.example.com" onmousedown="return rwt(this,'','','','1','adf','','ahahh','','',event)">lead by this<em>example</em></a>
</h3>
</div>
</div>
</div>
<div class="g">
<div class="abc">
<a class="picture" href="www.img.com"><img src="img" alt="image3"></a>
<div class="xyz">
<h3 class="_r">
<a class="title" href="www.example.com" onmousedown="return rwt(this,'','','','1','adf','','ahahh','','',event)">showed<em>example</em>for the people</a>
</h3>
</div>
</div>
</div>
<div class="g">
<div class="abc">
<a class="picture" href="www.img.com"><img src="img" alt="image4"></a>
<div class="xyz">
<h3 class="_r">
<a class="title" href="www.example.com" onmousedown="return rwt(this,'','','','1','adf','','ahahh','','',event)">we set<em>example</em>for people</a>
</h3>
</div>
</div>
</div>
</div>
当前,您将刮取的数据分配给循环中的
title
,然后分配给jlist的循环之外的title
。因此,循环完成后,title
的值始终是最后一个值
替换这个
for (Element e1 : e) {
title = e1.getElementsByTag("a").text();
}
DefaultListModel<String> listModel = new DefaultListModel<>();
listModel.addElement(title);
(元素e1:e)的{
title=e1.getElementsByTag(“a”).text();
}
DefaultListModel listModel=新的DefaultListModel();
listModel.addElement(标题);
用这个
DefaultListModel<String> listModel = new DefaultListModel<>();
for (Element e1 : e) {
listModel.addElement(e1.getElementsByTag("a").text());
}
DefaultListModel listModel=新的DefaultListModel();
用于(元素e1:e){
addElement(e1.getElementsByTag(“a”).text());
}
当前,您将刮取的数据分配给循环中的title
,然后分配给jlist的循环之外的title
。因此,循环完成后,title
的值始终是最后一个值
替换这个
for (Element e1 : e) {
title = e1.getElementsByTag("a").text();
}
DefaultListModel<String> listModel = new DefaultListModel<>();
listModel.addElement(title);
(元素e1:e)的{
title=e1.getElementsByTag(“a”).text();
}
DefaultListModel listModel=新的DefaultListModel();
listModel.addElement(标题);
用这个
DefaultListModel<String> listModel = new DefaultListModel<>();
for (Element e1 : e) {
listModel.addElement(e1.getElementsByTag("a").text());
}
DefaultListModel listModel=新的DefaultListModel();
用于(元素e1:e){
addElement(e1.getElementsByTag(“a”).text());
}
实际上,您不会每次都添加标题。循环使用找到的新值替换每次标题,并在循环后将其添加到列表中。像这样的东西可能会按照你想要的方式工作:
DefaultListModel<String> listModel = new DefaultListModel<>();
for (Element e1 : e) {
listModel.addElement(e1.getElementsByTag("a").text());
}
DefaultListModel listModel=新的DefaultListModel();
用于(元素e1:e){
addElement(e1.getElementsByTag(“a”).text());
}
实际上,您不会每次都添加标题。循环使用找到的新值替换每次标题,并在循环后将其添加到列表中。像这样的东西可能会按照你想要的方式工作:
DefaultListModel<String> listModel = new DefaultListModel<>();
for (Element e1 : e) {
listModel.addElement(e1.getElementsByTag("a").text());
}
DefaultListModel listModel=新的DefaultListModel();
用于(元素e1:e){
addElement(e1.getElementsByTag(“a”).text());
}
工作正常!:)。还有一个问题,我怎样才能获得标题的链接?根据,e1.getElementsByTag(“a”).attr(“href”)
应该可以做到这一点。@user7146946e1.getElementsByTag(“a”).attr(“href”)
工作得很好!:)。还有一个问题,我怎样才能得到标题的链接呢?根据,e1.getElementsByTag(“a”).attr(“href”)
应该可以做到这一点。@user7146946e1.getElementsByTag(“a”).attr(“href”)