gpt4 book ai didi

python - 尝试将 Discord 警报/ Hook 添加到现有的 python 代码中

转载 作者:太空宇宙 更新时间:2023-11-03 20:50:11 25 4
gpt4 key购买 nike

所以我想监控一个网页,如果发生更改,我希望通过 Discord 收到通知。

搜索谷歌,我到达了这个页面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)

最佳答案

您将需要使用discord API连接:使用python,使用discord.py。

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

关于python - 尝试将 Discord 警报/ Hook 添加到现有的 python 代码中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56318589/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com