python - 文件之间的 Python 全局变量
问题描述
我有一个包含我所有功能的 functions.py 文件。我想从 main.py 文件中打印一个变量,该文件是在 function.py 文件的函数中创建的。我怎样才能做到这一点?
函数.py:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Firefox()
def open_tradeview():
browser.get('https://www.tradingview.com/chart/')
wait = WebDriverWait(browser, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/div[3]/div/div/div/div"))).click() #Click on Hamburger menu
wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[6]/div/span/div[1]/div/div/div[11]"))).click() #Click on sign in button
wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[6]/div/div[2]/div/div/div/div/div/div/div[1]/div[4]/div/span"))).click() #Click on sign in with email button
wait.until(EC.visibility_of_element_located((By.NAME, "username"))).send_keys("trading_view_bot@fastmail.com") #type in email
wait.until(EC.visibility_of_element_located((By.NAME, "password"))).send_keys("2C3-cc9r" + Keys.RETURN) #type in password
time.sleep(18)
wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/div[1]/div[3]/div/div/div/article/button"))).click() #Click ad away
time.sleep(26)
wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/div[1]/div[3]/div/div/div/div/article/button"))).click() #Click ad away
time.sleep(5)
wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[6]/div[2]/div/div[2]/div/div/div[1]/button"))).click() #Click ad away
wait.until(EC.element_to_be_clickable((By.ID, "header-toolbar-symbol-search"))).click() #Currency pair
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".search-Hsmn_0WX.upperCase-Hsmn_0WX.input-3n5_2-hI"))).send_keys("VETUSD") #type in currency pair
wait.until(EC.element_to_be_clickable((By.XPATH, "//*[contains(text(), 'VeChain / US Dollar (calculated by TradingView)')]"))).click() #select new currency pair
wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/div[2]/div/div/div[1]/div[1]/div/div/div/div/div[2]/div/div"))).click() #click on time frame
wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[6]/div/span/div[1]/div/div/div/div[12]/div"))).click() #select 30min time frame
wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/div[2]/div/div/div[1]/div[1]/div/div/div/div/div[5]/div/div"))).click() #click on indicators
wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/div[2]/div/div/div[1]/div[1]/div/div/div/div/div[5]/div/div"))).click() #click on indicators
wait.until(EC.visibility_of_element_located((By.XPATH, "/html/body/div[6]/div/div/div[1]/div/div[2]/div/input"))).send_keys("ATR Trailing Stoploss") #search for ATR indicator
wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[6]/div/div/div[1]/div/div[3]/div[2]/div/div/div[2]/div[1]"))).click() #click on ATR indicator
wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[6]/div/div/div[1]/div/div[1]/span"))).click() #close search window
def get_atr_price():
global atr
global price
atr = browser.find_element_by_xpath('/html/body/div[2]/div[1]/div[2]/div[1]/div/table/tr[1]/td[2]/div/div[1]/div[2]/div[2]/div[2]/div[2]/div/div[1]/div').text
price = browser.find_element_by_xpath('/html/body/div[2]/div[1]/div[2]/div[1]/div/table/tr[1]/td[2]/div/div[1]/div[1]/div[2]/div[3]/span[2]').text
主要.py:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
import time
import yagmail
from functions import *
import functions
functions.get_atr_price()
open_tradeview()
get_atr_price()
print(atr)
当我想打印变量 atr 时,我收到一个错误,即未定义变量 atr
那么基本上我怎样才能从两个文件中访问每个变量呢?
解决方案
你试过这样的东西吗?
print(functions.atr)
这是一个对我有用的例子:
主要.py:
import functions
print("hello from main!")
print(functions.hello)
functions.funky() # this changes the value
print(functions.hello)
函数.py:
hello = 4 # here the global variable is declared
def funky():
print("hello from functions.funky()")
global hello
hello = 10 # changes the value
输出:
hello from main
4
hello from functions.funky()
10
编辑:如果你想在没有全局变量的情况下工作,你可以让get_atr_price()
函数返回 atr 和 price,如下所示:
函数.py:
def get_atr_price():
atr = browser.find_element_by_xpath('/html/body/div[2]/div[1]/div[2]/div[1]/div/table/tr[1]/td[2]/div/div[1]/div[2]/div[2]/div[2]/div[2]/div/div[1]/div').text
price = browser.find_element_by_xpath('/html/body/div[2]/div[1]/div[2]/div[1]/div/table/tr[1]/td[2]/div/div[1]/div[1]/div[2]/div[3]/span[2]').text
return atr, price
然后,在您的 main.py 文件中,您可以像这样使用它们:
atr, price = functions.get_atr_price();
推荐阅读
- spring-boot - 无法在关闭的连接上调用方法,在连接 tera 数据池的理想时间后面临此问题
- php - PHP shell_exec 一段时间后返回空字符串
- python - 从存储桶(Google Cloud Storage)获取文件类型 json 以上传并保存在 VM 实例中
- sqlite - Moor_flutter limit() 方法不起作用/被忽略
- java - 如何使用 jsoup 获取 img src?
- react-native - React Native - 本地图像在模拟器中运行得非常好,但在发布后却没有
- laravel - ERR_TOO_MANY_REDIRECTS Laravel
- scala - Scala & Play - 将参数传递给过滤器
- .net - BULK INSERT 错误:系统找不到指定的路径
- flutter - 如何计算 Flutter 上两个位置之间的距离?(结果应该是米)