Home > Back-end >  Looping a request 3 times check values and proceed the function
Looping a request 3 times check values and proceed the function

Time:07-27

Currently I am facing a logical problem on my code.

What I want to do is to request a site, get the JSON code and check the values. This is working fine and also the if and elif.

But now, I want to check for example the value '99' 3 times before I go for the do_value99() function.

Like: Request1 Value99 [CHECK] Request2 Value99 [CHECK] Request3 Value99 [CHECK] -> 3 times Value99 -> do_value99()

    response = requests.post('https://example.org', headers=headers, proxies=proxies,data=data)
    try:
        value = response.text.split('{"value')[1].split('|')[0]
        if value == "99":
            do_value99()
        elif value == "100":
            do_value100()
    except:
        time.sleep(1)

Anyone could help me out? I just need to know how the problem could be solved.

CodePudding user response:

# get the value
def get_response_value():
    response = requests.post('https://example.org', headers=headers, proxies=proxies, data=data)
    value = response.text.split('{"value')[1].split('|')[0]
    return value

def do_value99():
    pass

value_count = 0

while True:

    if value_count == 3:
        do_value99()
        value_count = 0

    if get_response_value() == "99":
        value_count  = 1

hope it helps

CodePudding user response:

Try this, it should check the value from response every 1 sec in infinite loop and if it find same value for 3 times it will call the function and will do the loop again to check value changes from response. If this what you want.

i = 0
j = 0
while True:
    response = requests.post('https://example.org', headers=headers, proxies=proxies, data=data)
    value = response.text.split('{"value')[1].split('|')[0]
    if value == "99":
        if i == 3:
            do_value99()
            time.sleep(1)
            i = 0
        else:
            i  = 1
            time.sleep(1)
    elif value == "100":
        if j == 3:
            do_value100()
            time.sleep(1)
            j = 0
        else:
            j  = 1
            time.sleep(1)
  • Related