簡體   English   中英

我如何擺脫循環?

[英]How can I get away from a loop?

所以我一直在嘗試使用Monitor-對於那些不知道什么是Monitor的人-基本上,這意味着您正在一段時間內檢查某些元素,URL或其他內容,然后再次檢查是否已改變。

這就是我所做的,並且...

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))

我遇到的問題是它在底部打印“ SENDING ... 2”-發生的是它一直一直在繼續打印出SENDING ... 2,這意味着它一直卡在循環中-

基本上,我想做的是,每當涉及第二個Else部分時,都應該將其打印一次,然后繼續“監視”並檢查直到有新的更改。 這意味着它將需要等到它出現在URL中/在URL中輸入密碼。

在這種情況下,我該如何實現?

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))
import requests
import random
import time

url = 'https://mrcnoir.com/account/login'
errState = False

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

    except requests.exceptions.RequestException as err:
        if not errState:            
            print('Error checking password page! - {}'.format(url) + ' - ' + str(err))
            print("SENDING...2")   # <-----------------
            errState = True
        continue

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

        if ('password' in password_page.text):
            # Password page is up
            print('Password page is up! - ' + '{}'.format(url))
            print("SENDING...1")
            errState = False


        #else:
        #    # Password page is down ???
        #    # this is not a useful test, if it is down you get an exception and
        #    # should handle it there
        #    print('Password page is down! - ' + '{}'.format(url))
        #    print("SENDING...2") #<---- If it comes in here - it will be stuck forever and just keep posting this print...

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM