inpu上的Python暂停循环

2024-04-18 04:46:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个循环正在处理一个动作块:

import threading
from selenium import webdriver
import os
from time import sleep
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import threading
import time, random

def get_player_prices(driver):
    lst_price = []
    #Determine list of first 20 cards
    lst_cards = driver.find_element_by_class_name("ui-layout-right") \
        .find_element_by_class_name("paginated-item-list") \
        .find_elements_by_xpath(".//*[contains(text(), 'Buy Now')]")
    #Loop through list of first 20 cards and determine prices
    for card in lst_cards:
        str_player_buy_now_value = card.find_element_by_xpath("..") \
            .find_element_by_class_name("currency-coins").text
        int_player_buy_now_value = int(str_player_buy_now_value.replace(",", "").strip())
        lst_price.append(int_player_buy_now_value)
    #Try with the next 20 cards
    try:
        btn_next = driver.find_element_by_xpath('/html/body/main/section/section/div[2]/div/div/section[2]/div[2]/section/div[2]/div/button[2]')
        btn_next.click()
        time.sleep(0.8)
        lst_cards = driver.find_element_by_class_name("ui-layout-right") \
            .find_element_by_class_name("paginated-item-list") \
            .find_elements_by_xpath(".//*[contains(text(), 'Buy Now')]")
        # Loop through list of first 20 cards and determine prices
        for card in lst_cards:
            str_player_buy_now_value = card.find_element_by_xpath("..") \
                .find_element_by_class_name("currency-coins").text
            int_player_buy_now_value = int(str_player_buy_now_value.replace(",", "").strip())
            lst_price.append(int_player_buy_now_value)
    except:
        return lst_price
    #Try to determine third 20 entries
    try:
        btn_next.click()
        time.sleep(0.8)
        lst_cards = driver.find_element_by_class_name("ui-layout-right") \
            .find_element_by_class_name("paginated-item-list") \
            .find_elements_by_xpath(".//*[contains(text(), 'Buy Now')]")
        # Loop through list of first 20 cards and determine prices
        for card in lst_cards:
            str_player_buy_now_value = card.find_element_by_xpath("..") \
                .find_element_by_class_name("currency-coins").text
            int_player_buy_now_value = int(str_player_buy_now_value.replace(",", "").strip())
            lst_price.append(int_player_buy_now_value)
    except:
        return lst_price
    #Try with the fourth 20 cards
    try:
        btn_next = driver.find_element_by_xpath('/html/body/main/section/section/div[2]/div/div/section[2]/div[2]/section/div[2]/div/button[2]')
        btn_next.click()
        time.sleep(0.8)
        lst_cards = driver.find_element_by_class_name("ui-layout-right") \
            .find_element_by_class_name("paginated-item-list") \
            .find_elements_by_xpath(".//*[contains(text(), 'Buy Now')]")
        # Loop through list of first 20 cards and determine prices
        for card in lst_cards:
            str_player_buy_now_value = card.find_element_by_xpath("..") \
                .find_element_by_class_name("currency-coins").text
            int_player_buy_now_value = int(str_player_buy_now_value.replace(",", "").strip())
            lst_price.append(int_player_buy_now_value)
    except:
        return lst_price
    #Try with the 5th 20 cards
    try:
        btn_next = driver.find_element_by_xpath('/html/body/main/section/section/div[2]/div/div/section[2]/div[2]/section/div[2]/div/button[2]')
        btn_next.click()
        time.sleep(0.8)
        lst_cards = driver.find_element_by_class_name("ui-layout-right") \
            .find_element_by_class_name("paginated-item-list") \
            .find_elements_by_xpath(".//*[contains(text(), 'Buy Now')]")
        # Loop through list of first 20 cards and determine prices
        for card in lst_cards:
            str_player_buy_now_value = card.find_element_by_xpath("..") \
                .find_element_by_class_name("currency-coins").text
            int_player_buy_now_value = int(str_player_buy_now_value.replace(",", "").strip())
            lst_price.append(int_player_buy_now_value)
    except:
        return lst_price
    #Try with the 6th 20 cards
    try:
        btn_next = driver.find_element_by_xpath('/html/body/main/section/section/div[2]/div/div/section[2]/div[2]/section/div[2]/div/button[2]')
        btn_next.click()
        time.sleep(0.8)
        lst_cards = driver.find_element_by_class_name("ui-layout-right") \
            .find_element_by_class_name("paginated-item-list") \
            .find_elements_by_xpath(".//*[contains(text(), 'Buy Now')]")
        # Loop through list of first 20 cards and determine prices
        for card in lst_cards:
            str_player_buy_now_value = card.find_element_by_xpath("..") \
                .find_element_by_class_name("currency-coins").text
            int_player_buy_now_value = int(str_player_buy_now_value.replace(",", "").strip())
            lst_price.append(int_player_buy_now_value)
    except:
        return lst_price
    #Try with the 7th 20 cards
    try:
        btn_next = driver.find_element_by_xpath('/html/body/main/section/section/div[2]/div/div/section[2]/div[2]/section/div[2]/div/button[2]')
        btn_next.click()
        time.sleep(0.8)
        lst_cards = driver.find_element_by_class_name("ui-layout-right") \
            .find_element_by_class_name("paginated-item-list") \
            .find_elements_by_xpath(".//*[contains(text(), 'Buy Now')]")
        # Loop through list of first 20 cards and determine prices
        for card in lst_cards:
            str_player_buy_now_value = card.find_element_by_xpath("..") \
                .find_element_by_class_name("currency-coins").text
            int_player_buy_now_value = int(str_player_buy_now_value.replace(",", "").strip())
            lst_price.append(int_player_buy_now_value)
    except:
        return lst_price
    #Try with the 8th 20 cards
    try:
        btn_next = driver.find_element_by_xpath('/html/body/main/section/section/div[2]/div/div/section[2]/div[2]/section/div[2]/div/button[2]')
        btn_next.click()
        time.sleep(0.8)
        lst_cards = driver.find_element_by_class_name("ui-layout-right") \
            .find_element_by_class_name("paginated-item-list") \
            .find_elements_by_xpath(".//*[contains(text(), 'Buy Now')]")
        # Loop through list of first 20 cards and determine prices
        for card in lst_cards:
            str_player_buy_now_value = card.find_element_by_xpath("..") \
                .find_element_by_class_name("currency-coins").text
            int_player_buy_now_value = int(str_player_buy_now_value.replace(",", "").strip())
            lst_price.append(int_player_buy_now_value)
    except:
        return lst_price
    #Try with the 9th 20 cards
    try:
        btn_next = driver.find_element_by_xpath('/html/body/main/section/section/div[2]/div/div/section[2]/div[2]/section/div[2]/div/button[2]')
        btn_next.click()
        time.sleep(0.8)
        lst_cards = driver.find_element_by_class_name("ui-layout-right") \
            .find_element_by_class_name("paginated-item-list") \
            .find_elements_by_xpath(".//*[contains(text(), 'Buy Now')]")
        # Loop through list of first 20 cards and determine prices
        for card in lst_cards:
            str_player_buy_now_value = card.find_element_by_xpath("..") \
                .find_element_by_class_name("currency-coins").text
            int_player_buy_now_value = int(str_player_buy_now_value.replace(",", "").strip())
            lst_price.append(int_player_buy_now_value)
    except:
        return lst_price
    #Try with the 10th 20 cards
    try:
        btn_next = driver.find_element_by_xpath('/html/body/main/section/section/div[2]/div/div/section[2]/div[2]/section/div[2]/div/button[2]')
        btn_next.click()
        time.sleep(0.8)
        lst_cards = driver.find_element_by_class_name("ui-layout-right") \
            .find_element_by_class_name("paginated-item-list") \
            .find_elements_by_xpath(".//*[contains(text(), 'Buy Now')]")
        # Loop through list of first 20 cards and determine prices
        for card in lst_cards:
            str_player_buy_now_value = card.find_element_by_xpath("..") \
                .find_element_by_class_name("currency-coins").text
            int_player_buy_now_value = int(str_player_buy_now_value.replace(",", "").strip())
            lst_price.append(int_player_buy_now_value)
    except:
        return lst_price

    return lst_price

options = Options()
#options.add_argument('headless')
options.add_argument('user-data-dir=C:\\Users\\timth\\AppData\\Local\\Google\\Chrome\\User Data\\')
driver = webdriver.Chrome(executable_path='C:\\Users\\timth\\Documents\\GitHub\\trade_helper\\chromedriver.exe', options=options)

# Open session
driver.get('https://www.xxxxxxxx')

time.sleep(10)

wait = WebDriverWait(driver, 3)


def wait_filters_set_up():
    # ask user to set up search filters
    while True:
        user_input_filters = input("Set up filters and type 'go' to continue\n")
        if user_input_filters != "go":
            print("Type 'go' to continue\n")
            continue
        else:
            break

def wait_user_start():
    # request user input start
    global bol_start_tool
    bol_start_tool = False
    while True:
        user_input_max_price = input('Enter max buy now price (>250):')
        if user_input_max_price.isdigit():
            int_user_input_max_price = int(user_input_max_price)
            if int_user_input_max_price > 250:
                break
            else:
                print('Max buy now price must be >250')
                continue
        else:
            print('Enter max buy now price (>250):')
            continue
    print(f'Max BIN set to {int_user_input_max_price} coins')
    time.sleep(3)

    while True:
        user_input_start = input("Press ENTER to start \n")
        if user_input_start != "":
            print("Press ENTER!")
            continue
        else:
            bol_start_tool = True
            break

def start_process():
    # actual start of PROCESS
    global bol_start_tool, continue_global, continue_process
    if bol_start_tool:
        continue_process = True

        thread_1 = threading.Thread(target=stop_or_restart)
        thread_1.daemon = True # without the daemon parameter, the function in parallel will continue even if your main program ends
        thread_1.start()

        print("Processing...")
        example_of_process()

def stop_or_restart():
    global continue_process, continue_global, count
    while continue_process:
        user_input = input("You can restart with R + Enter\n or stop with S + Enter \n")
        if user_input == 's':
            continue_process = False
            continue_global = False
            print("End of program, bye :)")
        if user_input == 'r':
            continue_process = False
            count = 0
            print("Restarting")

def example_of_process():
    global continue_process, count, int_user_input_max_price
    while continue_process:
        time.sleep(1)

        btn_search = driver.find_element_by_xpath(
            '/html/body/main/section/section/div[2]/div/div[2]/div/div[2]/button[2]')

        # filters.predefine_filters_in_search_mask()

        # set max price
        ipt_max_price = driver.find_element_by_xpath(
            "/html/body/main/section/section/div[2]/div/div[2]/div/div[1]/div[2]/div[6]/div[2]/input")
        ipt_max_price.click()
        time.sleep(1)
        ipt_max_price.send_keys(int_user_input_max_price)
        time.sleep(2)

        # Set min price
        int_players_bought = 0
        int_min_price = 200

        # Buy until 25 players were bought
        while int_players_bought < 100:

            if int_min_price >= int_user_input_max_price:
                int_min_price = 200

            ipt_min_price = wait.until(EC.element_to_be_clickable(
                (By.XPATH, '/html/body/main/section/section/div[2]/div/div[2]/div/div[1]/div[2]/div[5]/div[2]/input')))
            ipt_min_price.clear()
            ipt_min_price.click()
            time.sleep(0.5)
            ipt_min_price.send_keys(int_min_price)
            time.sleep(0.5)

            # click search button
            time.sleep(random.randint(1, 4))  # random sleep timer
            # print(f'Random delay is {}')
            btn_search = driver.find_element_by_xpath(
                '/html/body/main/section/section/div[2]/div/div[2]/div/div[2]/button[2]')
            btn_search.click()

            # if player is found, buy him. Else go back to search directly

            try:
                btn_buy_now = wait.until(EC.element_to_be_clickable((By.XPATH,
                                                                     '/html/body/main/section/section/div[2]/div/div/section[2]/div/div/div[2]/div[2]/button[2]')))
                btn_buy_now.click()
                confirm_button = wait.until(
                    EC.element_to_be_clickable((By.XPATH, '/html/body/div[1]/section/div/div/button[1]')))
                confirm_button.click()
                time.sleep(1)
                try:
                    time.sleep(1)
                    btn_price_check = wait.until(EC.element_to_be_clickable((By.XPATH,
                                                                             '/html/body/main/section/section/div[2]/div/div/section[2]/div/div/div[2]/div[3]/button[8]')))
                    btn_price_check.click()

                    lst_price = get_player_prices(driver)
                    int_min_player_buy_now_value = min(lst_price)
                    time.sleep(1)

                    # Determine + click Back button
                    btn_back_from_price_check = driver.find_element_by_xpath(
                        '/html/body/main/section/section/div[2]/div/div/section[2]/div[1]/button')
                    btn_back_from_price_check.click()
                    time.sleep(1)

                    # Determine player name + price payed
                    div_player_name = driver.find_element_by_xpath(
                        '/html/body/main/section/section/div[2]/div/div/section[1]/div/ul/li/div/div[1]/div[2]')
                    str_player_name = div_player_name.text
                    int_player_price = int(driver.find_element_by_xpath(
                        '/html/body/main/section/section/div[2]/div/div/section[2]/div/div/div[2]/div[1]/div[2]/div/span[2]').text.replace(
                        ",", ""))

                    # Determine + click List Player on Market button
                    time.sleep(1)
                    btn_list_player_on_market = driver.find_element_by_xpath(
                        '/html/body/main/section/section/div[2]/div/div/section[2]/div[2]/div/div[2]/div[2]/div[1]/button')
                    btn_list_player_on_market.click()
                    time.sleep(1)

                    # Determine + fill Start Price Input field
                    ipt_start_price = driver.find_element_by_xpath(
                        '/html/body/main/section/section/div[2]/div/div/section[2]/div[2]/div/div[2]/div[2]/div[2]/div[2]/div[2]/input')
                    ipt_start_price.click()
                    time.sleep(1)
                    ipt_start_price.send_keys("9999999999")
                    time.sleep(1)

                    # Determine + fill Buy Now Price Input field
                    ipt_buy_now_price = driver.find_element_by_xpath(
                        '/html/body/main/section/section/div[2]/div/div/section[2]/div[2]/div/div[2]/div[2]/div[2]/div[3]/div[2]/input')
                    ipt_buy_now_price.click()
                    time.sleep(1)

                    # Pricing

                    if int_min_player_buy_now_value > int_player_price:
                        ipt_buy_now_price.send_keys(str(int(int_min_player_buy_now_value)))
                        int_expected_profit = int((int_min_player_buy_now_value - int_player_price) * 0.95)
                    else:
                        ipt_buy_now_price.send_keys(str(int(int_player_price * 1.25)))
                        int_expected_profit = int(((int_player_price * 1.25) - int_player_price) * 0.95)
                    time.sleep(1)

                    # List player on market
                    btn_list_player = driver.find_element_by_xpath(
                        '/html/body/main/section/section/div[2]/div/div/section[2]/div[2]/div/div[2]/div[2]/div[2]/button')
                    btn_list_player.click()
                    # btn_send_to_transfer_list = wait.until(EC.element_to_be_clickable((By.XPATH, '/html/body/main/section/section/div[2]/div/div/section[2]/div/div/div[2]/div[3]/button[7]')))
                    # btn_send_to_transfer_list.click()
                    int_players_bought += 1  # increment players bought count
                    print(f'Bought player: {str_player_name} for {int_player_price}')
                    print(f'Estimated profit: {int_expected_profit}')
                    print(f'Player counter {str(int_players_bought)}/25')
                    time.sleep(1)
                    btn_back = wait.until(
                        EC.element_to_be_clickable((By.XPATH, '/html/body/main/section/section/div[1]/button[1]')))
                    btn_back.click()

                except:
                    time.sleep(1)
                    btn_back = wait.until(
                        EC.element_to_be_clickable((By.XPATH, '/html/body/main/section/section/div[1]/button[1]')))
                    btn_back.click()

            except:
                time.sleep(1)
                btn_back = wait.until(
                    EC.element_to_be_clickable((By.XPATH, '/html/body/main/section/section/div[1]/button[1]')))
                btn_back.click()

            time.sleep(0.5)
            if int_min_price < 1000:
                int_min_price += 50
            elif int_min_price < 10000:
                int_min_price += 100
            elif int_min_price < 100000:
                int_min_price += 250
            else:
                int_min_price += 1000


def main_function():
    global continue_global, count
    continue_global = True
    count = 0
    while continue_global:
        wait_filters_set_up()
        wait_user_start()
        start_process()

main_function()

开始“我的代码”的部分。。。是无限重复的

我需要找到一种方法,让用户有机会停止进程,也可以重新启动进程。你知道吗

重新启动应该从“#ask user to set up search filters”(要求用户设置搜索过滤器)开始,因为通常情况下,当用户想要停止、更改其搜索过滤器时,会出现这种情况。你知道吗

目前我没有使用GUI(它是palnned tho),所以我考虑设置两个用户输入选项,一个用于停止,一个用于重新启动。你知道吗

我使用的是带有chromedriver的selenium,因此完全关闭程序并手动重新启动将不是一个解决方案。从那时起,用户将不得不重新登录等,每次他想改变过滤器。你知道吗

有没有办法,我可以实现这两个函数到我的代码?你知道吗

或者你有什么建议我应该如何重新编写m代码使它工作?你知道吗

非常感谢您的帮助。你知道吗

编辑: @凤凰城这是我用你的代码做的。我已经更新了上面的孔代码。


Tags: divbyvaluesectionbuyelementfindprice
3条回答

将2个while循环包装到函数中。当你想离开这个函数的时候,你只需要返回它。每当你想重做这个过程,你只需再次调用这个函数。你知道吗

您需要一个全局while循环,以便更容易管理“重启”或“停止”。 我还将您的代码更改为函数,因此它更清晰。 一般来说,对于GUI并行操作,请查看threading

def wait_filters_set_up():
    # ask user to set up search filters
    while True :
        user_input_filters = input("Set up filters and type 'go' to continue\n")
        if user_input_filters != "go":
            print("Type 'go' to continue\n")
            continue
        else:
            break

def wait_user_start():
    # request user input start
    global bol_start_tool
    bol_start_tool = False
    while True:
        user_input_start = input("Press ENTER to start \n")
        if user_input_start != "":
            print("Press ENTER!")
            continue
        else:
            bol_start_tool = True
            break

def start_process():
    # actual start of PROCESS
    global bol_start_tool, continue_global
    if bol_start_tool:
        print("Processing...")

        continue_process = True
        while continue_process:
            user_input = input("You can restart with R + Enter\n or stop with S + Enter \n")
            if user_input == 's':
                continue_process = False
                continue_global = False
                print("End of program, bye :)")
            if user_input == 'r':
                continue_process = False
                print("Restarting")

def main_function():
    global continue_global
    continue_global = True
    while continue_global:
        wait_filters_set_up()
        wait_user_start()
        start_process()

main_function()

编辑:现在我使用一个线程来并行执行两个操作:主进程和等待用户输入(停止或重新启动)的函数。 这里我实现了一个基本计数器来模拟主进程。你知道吗

import threading
import time

def wait_filters_set_up():
    # ask user to set up search filters
    while True :
        user_input_filters = input("Set up filters and type 'go' to continue\n")
        if user_input_filters != "go":
            print("Type 'go' to continue\n")
            continue
        else:
            break

def wait_user_start():
    # request user input start
    global bol_start_tool
    bol_start_tool = False
    while True:
        user_input_start = input("Press ENTER to start \n")
        if user_input_start != "":
            print("Press ENTER!")
            continue
        else:
            bol_start_tool = True
            break

def start_process():
    # actual start of PROCESS
    global bol_start_tool, continue_global, continue_process
    if bol_start_tool:
        continue_process = True

        thread_1 = threading.Thread(target=stop_or_restart)
        thread_1.daemon = True # without the daemon parameter, the function in parallel will continue even if your main program ends
        thread_1.start()

        print("Processing...")
        example_of_process()

def stop_or_restart():
    global continue_process, continue_global, count
    while continue_process:
        user_input = input("You can restart with R + Enter\n or stop with S + Enter \n")
        if user_input == 's':
            continue_process = False
            continue_global = False
            print("End of program, bye :)")
        if user_input == 'r':
            continue_process = False
            count = 0
            print("Restarting")

def example_of_process():
    global continue_process, count
    while continue_process:
        print("count", count)
        count += 1
        time.sleep(1)

def main_function():
    global continue_global, count
    continue_global = True
    count = 0
    while continue_global:
        wait_filters_set_up()
        wait_user_start()
        start_process()

main_function()
need_to_stop = False

While !need_to_stop:    
    # ask user to set up search filters
    While True:
      user_input_filters = input("Set up filters and type 'go' to continue")
      # ***


    bol_start_tool = False

    # request user input start
    While True:
      user_input_start = input("Press ENTER to start")
      # ***

    # actual start of PROCESS
    if bol_start_tool:

    # MY CODE FOR PROCESS (searching according to the filters and further on working with the results)
    break; # <- this is your solution

相关问题 更多 >