首页 > 解决方案 > 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 个线程同时检查以查看网站是否已死,并将值作为打印返回,但我不确定这是否是我对问题的解决方案。

标签: pythonmultithreadingfor-looprequestmonitoring

解决方案


推荐阅读