如何监控对特定网站 CSS 的更改



我希望监控一些网站(这些网站不是我自己的网站(的CSS是否有更改,并在它们发生更改时收到某种通知。如果您能分享您对此的任何经验,为我指明如何编码的正确方向,我将不胜感激。

我希望这个脚本/应用程序在更改时通知 Slack 组,我认为这将需要一个 webhook。

不要求代码,只是询问有关特定 API 和其他可能有益的工具的任何建议。

我建议修改tschaefermedia的答案。

  1. 抓取网站以查找.css文件,保存。
  2. 取每个文件的 md5。
  3. 然后将新文件的md5与旧文件进行比较。
  4. 如果 md5 不同,则文件已更改。

下面是一个获取大文件的md5的功能。

def md5(file_name):
    # make a md5 hash object
    hash_md5 = hashlib.md5()
    # open file as binary and read only
    with open(file_name, 'rb') as f:
        i = 0
        # read 4096 bytes at a time and take the md5 hash of it and add it to the hash total
        # b converts string literal to bytes
        for chunk in iter(lambda: f.read(4096), b''):
            i += 1
            # get sum of md5 hashes
            # m.update(a); m.update(b) is equivalent to m.update(a+b)
            hash_md5.update(chunk)
        # check for correct number of iterations
        file_size = os.path.getsize(file_name)
        expected_i = int(math.ceil(float(file_size) / float(4096)))
        correct_i = i == expected_i
        # check if md5 correct
        md5_chunk_file = hash_md5.hexdigest()
    return md5_chunk_file

我建议在你的工作流程中使用Github。这使您可以很好地了解更改以及恢复到旧版本的方法。

一种可能的解决方案:

抓取网站以查找.css文件,保存更改日期和/或文件大小。

每次爬网后,比较信息,如果检测到更改,请使用 Slack API 进行通知。我没有使用过松弛,对于解决方案的这一部分,也许其他人可以提供建议。

最新更新