bash 检查网页是否已更新的简单脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9084453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:36:41  来源:igfitidea点击:

simple script to check if a webpage has been updated

bashwebscrape

提问by Programmer

There is some information that I am waiting for on a website. I do not wish to check it every hour. I want a script that will do this for me and notify me if this website has been updated with the keyword that I am looking for.

我在网站上等待一些信息。我不想每小时检查一次。我想要一个脚本来为我执行此操作,并在此网站已使用我正在寻找的关键字进行更新时通知我。

回答by Chris Seymour

Here is a basic bash script for checking if the webpage www.nba.comcontains the keyword Basketball. The script will output www.nba.com updated!if the keyword is found, if the keyword isn't found the script waits 10 minutes and checks again.

这是一个基本的 bash 脚本,用于检查网页www.nba.com 是否包含关键字Basketballwww.nba.com updated!如果找到关键字,脚本将输出,如果未找到关键字,脚本将等待 10 分钟并再次检查。

#!/bin/bash

while [ 1 ];
do
    count=`curl -s "www.nba.com" | grep -c "Basketball"`

    if [ "$count" != "0" ]
    then
       echo "www.nba.com updated!"
       exit 0   
    fi
    sleep 600   
done

We don't want the site or the keyword hard-coded into the script, we can make these arguments with the following changes.

我们不希望网站或关键字硬编码到脚本中,我们可以通过以下更改使这些参数。

#!/bin/bash

while [ 1 ];
do
    count=`curl -s "" | grep -c ""`

    if [ "$count" != "0" ]
    then
       echo " updated!"
       exit 0
    fi
    sleep 600
done

Now to run the script we would type ./testscript.sh www.nba.com Basketball. We could change the echocommand to have the script send an email or any other preferred way of notification. Note we should check that arguments are valid.

现在要运行脚本,我们将输入./testscript.sh www.nba.com Basketball. 我们可以更改echo命令,让脚本发送电子邮件或任何其他首选的通知方式。请注意,我们应该检查参数是否有效。

回答by naresh

go and configure a google alert..

去配置一个谷歌警报..

You can also crawl the website and search for the keyword you are interested in.

您还可以抓取网站并搜索您感兴趣的关键字。