代码之家  ›  专栏  ›  技术社区  ›  CDNthe2nd

我怎样才能摆脱循环呢?

  •  0
  • CDNthe2nd  · 技术社区  · 6 年前

    所以我一直在试着和monitor一起玩ABIT——对于那些不知道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”,这意味着它一直卡在循环中。-

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

    在这种情况下,我怎么能做到?

    2 回复  |  直到 6 年前
        1
  •  1
  •   OregonJim    6 年前
    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))
    
        2
  •  0
  •   hootnot    6 年前
    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))