How to repeat these commands in Python

Asked

Viewed 655 times

0

I would like to know how I make a command for the program to keep repeating this code a certain amount of times. I want him to repeat this code in one minute:

    driver.find_element_by_class_name("Ypffh").click()
    comment_input_box = driver.find_element_by_class_name("Ypffh")

    time.sleep(random.randint(2, 5))
    self.type_like_a_person(random.choice(comments), comment_input_box)

    time.sleep(random.randint(3, 5))
    driver.find_element_by_xpath("//button[contains(text(), 'Publicar')]").click()

    time.sleep(random.randint(3, 5))
    time.sleep(3)

I’ve tried to while, for and others. I want him to keep making a loop always on these commands above.

  • 1

    Dude, the only way to repeat this code is with the loops while or for. What exactly was your problem using them ?

  • I know it’s a bot. But my question is your script will just run this code repeatedly every minute or it will perform another activity while every minute it repeats this code?

3 answers

2

all right. I’ve been through a situation similar to yours, but it’s not exactly the same. What I could propose would be something like this:

import time

init_time = time.time()

while True:
    end_time = time.time()

    if end_time - init_time < 60:
        driver.find_element_by_class_name("Ypffh").click()
        comment_input_box = driver.find_element_by_class_name("Ypffh")

        time.sleep(random.randint(2, 5))
        self.type_like_a_person(random.choice(comments), comment_input_box)

        time.sleep(random.randint(3, 5))
        driver.find_element_by_xpath("//button[contains(text(), 'Publicar')]").click()

        time.sleep(random.randint(3, 5))
        time.sleep(3)

However, I must warn you that the time.sleep() can interfere with this count. Also, this will lock the flow of your application, in case Python is not asynchronous, like Node.js.

1

Good evening, if your idea is that it repeats itself from time to time, I believe that the best option is to use the Scheduler, which is a python Bib to schedule tasks, or a way that I do not find very interesting would be a while True and your stop flag could be a counter, for when it repeated n times it stopped. But I believe that still the best is the Scheduler.

conte = 0

while True:

    """código aqui dentro"""
    conte += 1
    sleep(60) # repete o código a cada 60 segundos
    if conte == n:
        break

n = number of times

I hope you have help with something.

0

You can try to create a function for this as follows:

Just going over: times represents how many times your code will run and intermission the time period between each execution.

def Repetir_meu_código(vezes, intervalo):

    for v in range(vezes):
        driver.find_element_by_class_name("Ypffh").click()
        comment_input_box = driver.find_element_by_class_name("Ypffh")

        time.sleep(random.randint(2, 5))
        self.type_like_a_person(random.choice(comments), comment_input_box)

        time.sleep(random.randint(3, 5))
        driver.find_element_by_xpath("//button[contains(text(), 'Publicar')]").click()

        time.sleep(intervalo)

Repetir_meu_código(10, 60) # Repetir 10 vezes a cada 60 segundos

Or simply use the loop for out of function as well:

The v inside the for represents how many times the loop has rotated, its count starts from the number 0 and goes to the value defined in crease() least 1. For example if the crease for 10 the last value of v will be 9 because in python the count starts from the 0. The name v can be exchanged for anything else like i for example or any other name.

for v in range(10):
    driver.find_element_by_class_name("Ypffh").click()
    comment_input_box = driver.find_element_by_class_name("Ypffh")

    time.sleep(random.randint(2, 5))
    self.type_like_a_person(random.choice(comments), comment_input_box)

    time.sleep(random.randint(3, 5))
    driver.find_element_by_xpath("//button[contains(text(), 'Publicar')]").click()

    time.sleep(60)

Doing this process in model function your you have more control over your code and can simply call the function again to be executed at another time or even import your script to use the function in another code.

This way you only matter the chosen function:

from nome_do_script import função_escolhida

Import all:

from nome_do_script import *

Or simply import all also requiring specification within the code:

import nome_do_script

Here however in all functions or classes that you are importing need to specify for your current code the source of function or class

nome_do_script_importado.função_importada()

Note that there is no need to include terminations (.py, . pyw and etc.) in import.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.