如何自动刷新网站和更新我时,它的页面源改变

How to auto-refresh website and update me when its page source changed?

本文关键字:改变 刷新 何自动 网站 更新      更新时间:2023-09-26

我期待着知道预订网站上什么时候会有新的"免费房间"。

我想我可以做一个刷新脚本,但我怎么能添加一个"如果"条件在一个变量在页面源更新我当它改变?这可能吗?

您可以尝试使用setInterval反复轮询变量更改,参见http://www.w3schools.com/jsref/met_win_setinterval.asp

编辑:如果你想定期下载整个页面,你可以这样做:

setInterval(function() {
    //this function is called repeatedly
    $.ajax("/path/to/your/page")
     .done(function(data, textStatus, jqXHR) {
         // check for changes and reload the page
      });
}, 1000);

谢谢大家的帮助。我设法使用这个简单的PHP脚本来提供我想要的东西,该脚本下载并搜索页面源代码中的字符串:

<?php
for ($i = 0; $i < 10000000000; ++$i) {
    $ch = curl_init("www.webpage.com");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $text = curl_exec($ch);
    $test = strpos($text, "Sorry, no rooms available.");
    if ($test)
    {
        echo "No rooms left.";  
    }
    else
    {
        echo 'ROOM AVAILABLE.'n';
        $exec = "terminal-notifier -message '"Room Available'"";
        shell_exec($exec);        
    }
    sleep(1);
}
?>

它使用terminal-notifier在OS x上向您发送通知。

https://github.com/julienXX/terminal-notifier

打开Terminal: :php script.php