python - Python - 如何使用请求和线程循环直到站点关闭
问题描述
所以我想做的是,我想做一种监控一个网站,它会接收一个随机数。在此之前,它需要向网站请求查看它是否有效。当它上线时,它会生成随机数 1-100,我希望它每隔 3-6 秒随机检查一次,然后再次打印该数字并重复,直到网站关闭。
我试图做的是:
def get_product_feed(url_site):
thread = url_site
password = False #We start by giving a false/true value
while not password: #If it is not True then we do the for loop
available = False
while not available:
try:
checkpass = requests.get(thread, timeout=12) #We requests the site to see if its alive or not
if ('deadsite' in checkpass.url): #If it contains etc deadsite then we enter the while True
while True:
contcheck = requests.get(thread,timeout=12) #We make new requests to see if its dead.
if ('deadsite' in contcheck.url): #if it is, then we sleep 3-7sec and try again
randomtime = random.randint(3, 7)
time.sleep(randomtime)
else: #If its not containig it anymore then we send the bs4 value
available = True
bs4 = soup(contcheck.text, 'lxml')
return bs4
break
else: #If its having either of them then we send instant.
bs4 = soup(contcheck.text, 'lxml')
return bs4
break
except Exception as err:
randomtime = random.randint(1, 2)
time.sleep(randomtime)
continue
def get_info(thread):
while True:
try:
url = thread
resp = requests.get(url, timeout=12) #We requests to the website here
resp.raise_for_status()
json_resp = resp.json() #We grab the json value.
except Exception as err:
randomtime = random.randint(1,3)
time.sleep(randomtime)
continue
metadata = json_resp['number'] #We return the metadata value back to get_identifier
return metadata
def get_identifier(thread):
new = get_info(thread) #We requests the get_info(thread):
try:
thread_number = new
except KeyError:
thread_number = None
identifier = ('{}').format(thread_number) #We return back to script
return identifier
def script():
url_site = 'https://www.randomsitenumbergenerator.com/' #What url we gonna use
old_list = []
while True:
for thread in get_product_feed(url_site): #We loop to see through get_product_feed if its alive or not
if get_identifier(thread) not in old_list: # We then ask get_identifier(thread) for the values and see if its in the old_list or not.
print(get_identifier(thread)
old_list.append(get_identifier(thread)
我添加了一条评论,以便更容易理解发生了什么。
我现在遇到的问题是,在网站关闭之前我无法让get_identifier(thread)运行,我希望它继续打印出来,直到网站上线直到它死掉,这是我的问题!我需要做什么才能实现它?
我的想法是最终添加线程,可能有 10 个线程同时检查以查看网站是否已死,并将值作为打印返回,但我不确定这是否是我对问题的解决方案。
解决方案
推荐阅读
- javascript - 重构 nodejs 模块
- css - Antd 黑暗主题覆盖了我自己的 css。如何解决?
- javascript - 单选按钮 onChange 事件
- neo4j - 密码查询以将图形作为对象返回,其中每个父节点都包含其子节点的数组?
- kubernetes - microk8s 中的 Traefik 总是 404 通过 HTTPS
- ios - 如何在 AppDelegate 中初始化 Core Data 的持久化容器并在整个应用程序中使用它?
- c++ - C++:实现 ln(a) 的数值逼近
- python - VBA运行python脚本 - 权限错误
- python - 根据等于熊猫数据框中特定值的列定位最小日期?
- python - 在将字符串添加到电子邮件时获取 \n