我如何摆脱循环?

时间:2018-06-17 13:47:45

标签: python loops

所以我一直试图用监视器来玩游戏 - 对于那些不知道监控是什么的人 - 基本上是什么意思是你在某个时间检查某些元素,网址或其他什么,然后再检查一下它已被改变。

这就是我所做的......

url = 'mrcnoir'

while True:
        try:
            password_page = requests.get('https://{}.com'.format(url), timeout=5)
            password_page.raise_for_status()

        except requests.exceptions.RequestException as err:
            print('Error checking password page! - https://{}.com'.format(url) + ' - ' + str(err))
            continue



        else:
            # *************---If password up---**************

            if ('password' in password_page.url):
                        # Password page is up
                        print('Password page is up! - ' + 'https://{}.com'.format(url))
                        if not ('password' in password_page.url):

                            # No password page -> password page


                            # *************---Send---**************

                            print("SENDING...1")

                            time.sleep(random.randint(6, 12))


            # *************---If password down---**************

            else:
                # Password page is down
                print('Password page is down! - ' + 'https://{}.com'.format(url))
                if ('password' in password_page.url):

                    # Password page -> no password page


                    # *************---Send---**************

                    print("SENDING...2") #<---- If it comes in here - it will be stuck forever and just keep posting this print...
                    time.sleep(random.randint(6, 12))

        # *************---Retry between 6-12 random.---**************
        finally:
            time.sleep(random.randint(6, 12))

我所拥有的问题是打印出“发送... 2”的底部 - 发生的事情是它只是继续打印出SENDING ... 2,这意味着它一直卡在循环中 -

基本上我想做的是,每当涉及到第二个Else部分时,它应该打印出来一次,然后继续“监视”并检查直到有新的变化。这意味着需要等到它出现/密码在网址中。

在这种情况下,我怎么能实现呢?

2 个答案:

答案 0 :(得分:1)

url = 'mrcnoir'
last_status = False
while True:
        try:
            password_page = requests.get('https://{}.com'.format(url), timeout=5)
            password_page.raise_for_status()

        except requests.exceptions.RequestException as err:
            print('Error checking password page! - https://{}.com'.format(url) + ' - ' + str(err))
            continue

        else:
            # *************---If password up---**************

            if 'password' in password_page.url and last_status == False:
                # Password page is up
                last_status = True
                print('Password page is up! - ' + 'https://{}.com'.format(url))

                time.sleep(random.randint(6, 12))


            # *************---If password down---**************

            elif not 'password' in password_page.url and last_status == True:
                # Password page is down
                last_status = False
                print('Password page is down! - ' + 'https://{}.com'.format(url))
                time.sleep(random.randint(6, 12))

        # *************---Retry between 6-12 random.---**************
        finally:
            time.sleep(random.randint(6, 12))

答案 1 :(得分:0)

sudo apt install mdbtools