尝试向现有python cod添加不一致警报/挂钩

2024-05-16 05:13:35 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我想监控一个网页,如果发生了变化,我希望通过Discord得到通知。你知道吗

搜索google,我登陆了这个页面https://www.adventuresintechland.com/detect-when-a-webpage-changes-with-python/

当我在我的网站上测试它时,它似乎起作用了。 但我想添加不和谐的警报到这个代码,但似乎被卡住了

我研究了github上的Dhooks,一直在尝试实现它

# Hunter Thornsberry
# http://www.adventuresintechland.com

# WebChange.py
# Alerts you when a webpage has changed it's content by comparing checksums of the html.

import hashlib
import urllib2
import random
import time

# url to be scraped
url = "http://raw.adventuresintechland.com/freedom.html"

# time between checks in seconds
sleeptime = 60

def getHash():
    # random integer to select user agent
    randomint = random.randint(0,7)

    # User_Agents
    # This helps skirt a bit around servers that detect repeaded requests from the same machine.
    # This will not prevent your IP from getting banned but will help a bit by pretending to be different browsers
    # and operating systems.
    user_agents = [
        'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11',
        'Opera/9.25 (Windows NT 5.1; U; en)',
        'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)',
        'Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.5 (like Gecko) (Kubuntu)',
        'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.142 Safari/535.19',
        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:11.0) Gecko/20100101 Firefox/11.0',
        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:8.0.1) Gecko/20100101 Firefox/8.0.1',
        'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.151 Safari/535.19'
    ]

    opener = urllib2.build_opener()
    opener.addheaders = [('User-agent', user_agents[randomint])]
    response = opener.open(url)
    the_page = response.read()

    return hashlib.sha224(the_page).hexdigest()

current_hash = getHash() # Get the current hash, which is what the website is now

while 1: # Run forever
    if getHash() == current_hash: # If nothing has changed
        print "Not Changed"
    else: # If something has changed
        print "Changed"
        break
    time.sleep(sleeptime)

Tags: thetoimportcomurlmozillatimewindows
2条回答

您将需要使用discordapi连接:对于python,使用不和谐.py. 你知道吗

看看discord.py docs并学习如何使用它,然后将两者连接起来-只需添加所需的不和谐.py在适当的位置导入并设置文件,并在网页更改时添加发送消息的提示(在本例中,将print("changed")替换为yourChannel.send("webpage has changed!)")。你知道吗

您需要连接到discord API才能在事件中发送消息。信息可以在herehere中找到。你知道吗

值得注意的是,discord python库目前仅与python3.6兼容,因此您必须运行

pip install -U discord.py 

在Python3.6而不是Python3.7上。你知道吗

相关问题 更多 >