我正在开发一个项目,需要解析 HTML 以从网页中提取数据。我在 Java 中使用 Jsoup。我需要从以下内容中提取数据。
<tr>
<td><small><a href="http://www.timeanddate.com/worldclock/fixedtime.html?iso=20160821T2100&p1=248" target="_blank">2016/08/21 21:00</a></small></td>
<td><small><a href="https://agc003.contest.atcoder.jp">AtCoder Grand Contest 003</a></small></td>
</tr>
我可以获取值“竞赛名称”和“时间”,但如何提取 URL。我想获取比赛网址 https://agc003.contest.atcoder.jp
如何得到这个??
编辑: 这是我的代码
private void getAC() 抛出 IOException {
文档 doc = Jsoup.connect("https://atcoder.jp/").userAgent(Desktop.getDesktop().toString()).get();
元素表 = doc.getElementsByClass("table-responsive").get(1);
元素竞赛开始时间 = table.getElementsByTag("td");
整数cnt = 1;
for (元素 i : 竞赛开始时间) {
System.out.println(cnt + "."+ i.html());
cnt++;
}
}
最佳答案
JSoup 有丰富的 DOM 处理 API,寻找这个函数:
Element content = doc.getElementById("content");
Elements links = content.getElementsByTag("a");
for (Element link : links) {
String linkHref = link.attr("href");
String linkText = link.text();
}
您也可以通过这种方式获取链接
Elements links = doc.select("table a[href]");
关于java - 解析 HTML href 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39035059/