Python 在输入时暂停循环
Python Pause loop on input
我有一个正在处理动作块的循环:
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()
开始 #MY CODE ... 的部分正在无限重复,
我需要找到一种方法,让用户有机会停止进程并重新启动进程。
重启应该从“# ask user to set up search filters”开始,因为通常这种情况会在用户想要停止时出现,以更改他的搜索过滤器。
目前我没有使用 GUI(虽然已计划)所以我考虑设置两个用户输入选项,一个用于停止,一个用于重新启动。
我将 selenium 与 chromedriver 一起使用,因此完全关闭程序并手动重新启动不是解决方案。从那时起,用户每次想要更改过滤器时都必须重新登录等。
有什么办法可以在我的代码中实现这两个功能吗?
或者您有什么建议我必须重新编写 m 代码才能使其工作吗?
非常感谢任何帮助。
编辑:
@phoenix 这就是我对你的代码所做的。我已经更新了上面的孔代码。
将 2 个 while 循环包装在一个函数中。然后,每当您想离开该功能时,只需 return 即可。每当您想重做该过程时,只需再次调用该函数即可。
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
您需要一个全局 while 循环,以便更容易管理 "Restart" 或 "Stop"。
我还将您的代码更改为 functions,因此 更清晰。
对于一般的 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()
EDIT :现在我使用一个线程并行执行 2 个操作:您的主进程和一个正在等待用户输入(停止或重新启动)的函数。
这里我实现了一个基本的计数器来模拟你的主进程。
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()
我有一个正在处理动作块的循环:
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()
开始 #MY CODE ... 的部分正在无限重复,
我需要找到一种方法,让用户有机会停止进程并重新启动进程。
重启应该从“# ask user to set up search filters”开始,因为通常这种情况会在用户想要停止时出现,以更改他的搜索过滤器。
目前我没有使用 GUI(虽然已计划)所以我考虑设置两个用户输入选项,一个用于停止,一个用于重新启动。
我将 selenium 与 chromedriver 一起使用,因此完全关闭程序并手动重新启动不是解决方案。从那时起,用户每次想要更改过滤器时都必须重新登录等。
有什么办法可以在我的代码中实现这两个功能吗?
或者您有什么建议我必须重新编写 m 代码才能使其工作吗?
非常感谢任何帮助。
编辑: @phoenix 这就是我对你的代码所做的。我已经更新了上面的孔代码。
将 2 个 while 循环包装在一个函数中。然后,每当您想离开该功能时,只需 return 即可。每当您想重做该过程时,只需再次调用该函数即可。
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
您需要一个全局 while 循环,以便更容易管理 "Restart" 或 "Stop"。 我还将您的代码更改为 functions,因此 更清晰。 对于一般的 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()
EDIT :现在我使用一个线程并行执行 2 个操作:您的主进程和一个正在等待用户输入(停止或重新启动)的函数。 这里我实现了一个基本的计数器来模拟你的主进程。
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()