Jsoup怎么把如下document里所有的标题和链接提取出来? 10
如下图所示,对HTML进行分析得到指定class="articleh"里的所有内容,怎么对他们进行处理,因为一个class里对应一个herf和一个title,想把他们分别...
如下图所示,对HTML进行分析得到指定class="articleh"里的所有内容,怎么对他们进行处理,因为一个class里对应一个herf和一个title,想把他们分别提取出来,并存在一个数组里,对应的数组应是:
a[0][0] = news,cjpl,115144358.html ; a[0][1] = 国务院鼓励险资支持股市稳定发展;
a[1][0] = news,cjpl,115095097.html ;a[1][1] = 周三A股市场缘何大跌;
麻烦各位大神给予指导,谢谢!! 展开
a[0][0] = news,cjpl,115144358.html ; a[0][1] = 国务院鼓励险资支持股市稳定发展;
a[1][0] = news,cjpl,115095097.html ;a[1][1] = 周三A股市场缘何大跌;
麻烦各位大神给予指导,谢谢!! 展开
展开全部
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class Test {
public static void main(String[] args) throws IOException {
Document doc = Jsoup.parse(new File("E:/a.html"), "UTF-8");
Elements els = doc.select("div.articleh span.l3 a");
List<Article> articles = new ArrayList<Article>();
for (Element el : els) {
articles.add(new Article(el.attr("href"), el.attr("title")));
}
for (Article article : articles) {
System.out.println(article.getTitle());
System.out.println(article.getHref());
System.out.println();
}
}
}
class Article {
private String href;
private String title;
public Article(String href, String title) {
this.href = href;
this.title = title;
}
public String getHref() {
return href;
}
public void setHref(String href) {
this.href = href;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class Test {
public static void main(String[] args) throws IOException {
Document doc = Jsoup.parse(new File("E:/a.html"), "UTF-8");
Elements els = doc.select("div.articleh span.l3 a");
List<Article> articles = new ArrayList<Article>();
for (Element el : els) {
articles.add(new Article(el.attr("href"), el.attr("title")));
}
for (Article article : articles) {
System.out.println(article.getTitle());
System.out.println(article.getHref());
System.out.println();
}
}
}
class Article {
private String href;
private String title;
public Article(String href, String title) {
this.href = href;
this.title = title;
}
public String getHref() {
return href;
}
public void setHref(String href) {
this.href = href;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询