2
我正在開發一個項目,我需要解析HTML以從網頁中提取數據。我在Java中使用Jsoup。我需要從以下內容中提取數據。解析HTML href屬性
<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() throws IOException {
Document doc = Jsoup.connect("https://atcoder.jp/").userAgent(Desktop.getDesktop().toString()).get();
Element table = doc.getElementsByClass("table-responsive").get(1);
Elements contestStartTime = table.getElementsByTag("td");
int cnt = 1;
for (Element i : contestStartTime) {
System.out.println(cnt + ". " + i.html());
cnt++;
}
}
我不是f熟悉JSoup或Java,但我會加載文件,逐行讀取它並使用正則表達式模式來搜索您需要的,然後從該行解析Url。 – dinotom
你可以添加你的代碼來獲取比賽名稱和時間嗎? – TDG
由於標籤似乎沒有一個id或anyhing目標與他們,我真的不知道。但是,一旦找到元素就很容易獲取URL。 'Elements.attr(「href」)'應該得到值 –