投资机器人
我正在尝试创建一个机器人,自动投资于巴西的存款证(CD)。这些存款证是由特定的股票经纪公司提供的,但它们的存在时间甚至不到4秒。下面的代码是指在手动输入登录名、密码和令牌之后的自动步骤。当我运行程序时,输入登录名后,在输入密码之前,网站就阻止了我的访问。我该如何解决这个问题呢?
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
# Investment page URL
investments_url = "https://experiencia.xpi.com.br/renda-fixa/#/emissao-bancaria"
# WebDriver configuration (you need to have ChromeDriver or another driver installed)
driver = webdriver.Chrome()
# Open the browser and manually login if necessary
input("Please login and press Enter to continue...")
# Function to perform the investment process
def perform_investment():
# Navigate to the investments page
driver.get(investments_url)
# Wait for the investments page to load
time.sleep(2)
# Find all available investments in the table
investments_table = driver.find_element_by_css_selector(".sc-bkEOxz.jStPvy.soma-table-body.hydrated")
rows = investments_table.find_elements_by_xpath(".//tr")
# Check each investment
for row in rows:
# Extract investment information
asset = row.find_element_by_xpath(".//td[1]").text
issuer = row.find_element_by_xpath(".//td[2]").text
profitability = float(row.find_element_by_xpath(".//td[3]").text.replace('%', '').replace(',', '.'))
# Check if the investment meets the specified criteria
if "SICREDI" in asset.upper() and profitability > 12.50:
print(f"Investment found: {asset}, Issuer: {issuer}, Profitability: {profitability}%")
# Click the "Invest" button on the table row where the investment was identified
invest_button = row.find_element_by_xpath(".//button[contains(text(),'Investir')]")
invest_button.click()
# Wait for the next page to load
time.sleep(2)
# Fill in the desired quantity (in this example, we fill in with 2)
quantity_input = driver.find_element_by_xpath("//input[@id='input_quantidade']")
quantity_input.send_keys("2")
# Click on "Advance step"
next_button = driver.find_element_by_xpath("//button[contains(text(),'Avançar etapa')]")
next_button.click()
# Click 6 times on the button with the numeral "0" (for electronic signature)
for _ in range(6):
button_0 = driver.find_element_by_xpath("//button[contains(text(),'0')]")
button_0.click()
time.sleep(0.1) # Small interval to ensure clicks are processed
# Click on "Advance step" to finalize the investment
final_next_button = driver.find_element_by_xpath("//button[contains(text(),'Avançar etapa')]")
final_next_button.click()
print("Investment successfully made!")
# No need to continue checking investments after making one
return
# Perform the investment process
perform_investment()
# Close the browser
driver.quit()
我希望能够访问网页“https://experiencia.xpi.com.br/renda-fixa/#/emissao-bancaria”,并在银行SICREDI的投资出现后立即完成投资,自动选择数量为2,并输入我的电子签名。
0 个回答
暂无回答