我正在用 Java 创建一个应用程序来检查网页是否已更新。
但是有些网页没有“上次修改”标题。
我什至尝试检查内容长度的变化,但这种方法并不可靠,因为有时内容长度发生变化而网页没有任何修改,从而发出误报。
我真的需要一些帮助,因为我想不出一个万无一失的方法。
有什么想法吗???
最佳答案
如果你像这段代码一样一直连接到网页,它可以帮助:
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class main {
String updatecheck = "";
public static void main(String args[]) throws Exception {
//Constantly trying to load page
while (true) {
try {
System.out.println("Loading page...");
// connecting to a website with Jsoup
Document doc = Jsoup.connect("URL").userAgent("CHROME").get();
// Selecting a part of this website with Jsoup
String pick = doc.select("div.selection").get(0);
// printing out when selected part is updated.
if (updatecheck != pick){
updatecheck = pick;
System.out.println("Page is changed.");
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("Exception occured.... going to retry... \n");
}
}
}
}
How to get notified after a webpage changes instead of refreshing?
关于java - 如果网页已更新,则发出警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7453823/