简体   繁体   English

我如何摆脱循环?

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

So I have been trying to play around abit with monitor - For those who doesn't know what monitoring is - basically what it means is that you are checking something etc element, url or whatever in a certain time and check again if it has been changed. 所以我一直在尝试使用Monitor-对于那些不知道什么是Monitor的人-基本上,这意味着您正在一段时间内检查某些元素,URL或其他内容,然后再次检查是否已改变。

This is what I have done and... 这就是我所做的,并且...

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

The issue im having is at the bottom where it prints "SENDING...2" - What happened is that it just continues to print out the SENDING...2 all the time which means it been stuck in the loop - 我遇到的问题是它在底部打印“ SENDING ... 2”-发生的是它一直一直在继续打印出SENDING ... 2,这意味着它一直卡在循环中-

Basically what I want to do is that whenever it comes to the second Else part it should print it out once and then continue to "monitor" and check until there is new changes. 基本上,我想做的是,每当涉及第二个Else部分时,都应该将其打印一次,然后继续“监视”并检查直到有新的更改。 Meaning it will be needing to wait until it appears/password in the url. 这意味着它将需要等到它出现在URL中/在URL中输入密码。

How could I in that case make it happen? 在这种情况下,我该如何实现?

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.

相关问题 如何在循环中从randint获得新结果? - How can I get new results from randint while in a loop? 如何从循环中获取 tkinter 条目 - How can I get the tkinter entry from a loop 我怎样才能让我的循环从“第一天”开始? - How can i get my loop to start from "Day 1"? 如何从 plot 上切下一块并将我需要的点设置为零? - How can I cut a piece away from a plot and set the point I need to zero? 我怎样才能把time.ctime从time.ctime拿走 - how can i take time.ctime away from time.ctime 我怎样才能从日期时间戳记中取出一纳秒,并在日期时间戳记中获得最终形式? - How can I take one nanosecond away from a datetime timestamp, and have the final form in datetime timestamp? 我创建了一个机器人可以改变我的角色,但它会出错 - i create a bot can change my roles away but it get err 如何生成三个异常点,使它们明显远离 python 中的正常数据? - How can i generate three outlier points such that they are apparently far away from the normal data in python? 如何使用 `return` 从 for 循环中取回多个值? 我可以把它们放在一个列表中吗? - How can I use `return` to get back multiple values from a for loop? Can I put them in a list? 如何获得“for”循环的前 3 个结果? - How can I get the first 3 results of a 'for' loop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM