gpt4 book ai didi

java - 如何使用 Jsoup 从定义的 URL 中查找时间?

转载 作者:行者123 更新时间:2023-11-30 08:34:01 25 4
gpt4 key购买 nike

我正在使用 Java 并使用 Jsoup。我想从下面的 url 中找到 Time。我试过这些但无法得到它。时间位于 span 标签中,但在 id(timestamp--time timeago) 下 我正在尝试使用它,但我不知道是什么问题。

Document doc;
Elements lin = null;
String url = "http://www.dawn.com/news/1277133/this-is-how-pakistanis-around-the-country-are-celebrating-independence-day";
try {
doc = Jsoup.connect(url).timeout(20*1000).userAgent("Chrome").get();
lin = doc.getElementsByClass("span.timestamp--time timeago");
// System.out.println(lin);
} catch (IOException e) {
e.printStackTrace();
}
int i=0;
for(Element l :lin){
System.out.println(""+i+ " : " +l.text());
i++;
}

最佳答案

替换

lin = doc.getElementsByClass("span.timestamp--time  timeago");

lin = doc.select("span.timestamp--time.timeago");

输出:

0 : Aug 14, 2016 04:29pm
1 : Aug 14, 2016 12:39pm
2 : Aug 14, 2016 01:34pm
3 : Aug 14, 2016 01:37pm
4 : Aug 14, 2016 04:19pm
5 : Aug 15, 2016 01:11am
6 : Aug 15, 2016 08:49am
7 : Aug 16, 2016 12:32pm

来自API中的措辞getElementsByClass 只接受一个类名。所以在这种情况下 lin = doc.getElementsByClass("timestamp--time"); 给出了相同的输出,但是如果你想按多个类进行选择而不是 select是更好的选择。

关于java - 如何使用 Jsoup 从定义的 URL 中查找时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39108427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com