javascript - 如何从脚本标签和/或控制台中抓取值

标签 javascript python html web-scraping

我有一个在 heroku/flask 上运行的 python 程序。基本上,我收到了两个 webhook。 第一个 webhook 启动秒表。 第二个 webhook 应该获取秒表的当前值。

我正在努力获取秒表的当前值。似乎有两种选择。通过网络抓取从实际变量中获取值,或者将值打印到控制台并从那里获取它们。我正在考虑使用 Beautiful soup,但不确定如何在秒表运行时获取实际值。

下面的代码向您展示了我使用 python 呈现的 html 文件。 如何在秒表运行时从程序中获取特定的“sec”变量?或者我如何获取正在打印到控制台的信息?

<!DOCTYPE html>
<html>
<body>
    <div id="stopwatch-container">
        <div id="stopwatch">00:00:00</div>
        <button onclick="pause()">Stop</button>
    </div>
    <script>
        var stopwatch = document.getElementById("stopwatch");
        var startBtn = document.getElementById("start-btn");
        var timeoutId = null;
        var ms = 0;
        var sec = 0;
        var min = 0;
        start();
        
        /* function to start stopwatch */
        function start(flag) {
            if (flag) {
                startBtn.disabled = true;
            }
 
            timeoutId = setTimeout(function() {
                ms = parseInt(ms);
                sec = parseInt(sec);
                min = parseInt(min);
                console.log(sec) // here you can see I am logging the sec value. 
 
                ms++;
 
                if (ms == 100) {
                    sec = sec + 1;
                    ms = 0;
                }
                if (sec == 60) {
                    min = min + 1;
                    sec = 0;
                }
                if (ms < 10) {
                    ms = '0' + ms;
                }
                if (sec < 10) {
                    sec = '0' + sec;
                }
                if (min < 10) {
                    min = '0' + min;
                }
 
                stopwatch.innerHTML = min + ':' + sec + ':' + ms;
 
                // calling start() function recursivly to continue stopwatch
                start();
 
            }, 10); // setTimeout delay time 10 milliseconds
        }
 
        /* function to pause stopwatch */
        function pause() {
            clearTimeout(timeoutId);
            startBtn.disabled = false;
        }
 
        /* function to reset stopwatch */
        function reset() {
            ms = 0;
            sec = 0;
            min = 0;
            clearTimeout(timeoutId);
            stopwatch.innerHTML = '00:00:00';
            startBtn.disabled = false;
        }
    </script>
</body>
</html>

最佳答案

<!DOCTYPE html>
<html>
<body>
    <div id="stopwatch-container">
        <div id="stopwatch">00:00:00</div>
        <button onclick="pause_timer()">Stop</button>
        <button onclick="start_timer()">Start</button>
    </div>
    <script>
        var stopwatch = document.getElementById("stopwatch");
        var startBtn = document.getElementById("start-btn");
        const date = new Date;
        var second = 0;
        var minute = 0;
        var hour = 0;
        var second_timer;
        var pause=true;
        function seconds(){
            if(pause==false){
                second++;
                if(second>=60){second=0;minute++;}
                if(minute>=60){minute=0;hour++;}
                if(hour>=24){hour=0;}
                if(second<10){second="0"+second.toString();}
                if(minute<10){minute="0"+minute.toString();}
                if(hour<10){hour="0"+hour.toString();}
                stopwatch.innerText = hour+":"+minute+":"+second;
                second=parseInt(second);
                minute=parseInt(minute);
                hour=parseInt(hour);
            }
        }
        function start_timer(){
            var second_timer=setInterval(seconds,1000);
            pause=false;
        }
        function pause_timer(){
            clearInterval(second_timer);
            pause=true;
        }
    </script>
</body>
</html>

关于javascript - 如何从脚本标签和/或控制台中抓取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73808842/

相关文章:

python - 调用多个方法时,会添加吗?

html - CSS3 旋转回弹

javascript - 如何一次记录多个变量?

html - 宽度样式属性在 html 中没有得到正确的尊重?

javascript - 使用 React Router,我如何为 HTML 元素分配一个类?

javascript - 如何忽略用于测试 webpack 的原始加载器

python - 关闭 DictReader 有必要还是可能?

python - 如何检测 Pygame 中的两个图像是否接触?

javascript - 如何在没有 JavaScript 中的 activeX 对象的情况下检查 IE 5 中的文件大小?

javascript - Node JS Mongoose 在 for 或 foreach 循环中更新查询,这可能吗?