![](/img/trans.png)
[英]Python, Kivy: Problem with calling functions from different classes/screens
[英]Combing and calling Functions from different files problem
我對 python 很陌生,但我設法使用 selenium 創建了一些功能性代碼。 我正在嘗試擴展代碼,同時使其模塊化。
因此,該程序會訪問 30 個網站並抓取一些數據並將其返回。 我現在要做的是在單獨的 .py 文件中創建一個函數,其中包含每個網站抓取的代碼。 然后像這樣調用 main.py 上的文件和函數:
import Functions
functions.test_func()
def test_func():
from selenium import webdriver
driver = webdriver.Chrome(executable_path='C:\drivers\chromedriver_win32\chromedriver.exe')
import time
driver.get('https://reddit.com')
time.sleep(5)
driver.close()
這很好用,我很滿意,但當然,如果我可以有一個外部函數,如果我需要而不是在 30 個文件中的每個文件中更改它會更容易
def webdriver():
from selenium import webdriver
driver = webdriver.Chrome(executable_path='C:\drivers\chromedriver_win32\chromedriver.exe')
取代
def test_func():
>>>from selenium import webdriver
driver = webdriver.Chrome(executable_path='C:\drivers\chromedriver_win32\chromedriver.exe')<<<
import time
driver.get('https://reddit.com')
time.sleep(5)
driver.close()
# - - # 和
def test_func():
import web_driver_func
web_driver_func.webdriver()
import time
driver.get('https://reddit.com')
time.sleep(5)
driver.close()
但是“驅動程序”沒有定義,我理解其中的邏輯。 但是我一直在嘗試尋找一種我尚未遇到的解決方案或方法來解決它。 我想如果我這樣寫並從 test_func() 中排除它會起作用,但它是同樣的問題:
from selenium import webdriver
import Functions
driver = webdriver.Chrome(executable_path='C:\drivers\chromedriver_win32\chromedriver.exe')
Functions.test_func()
如果我的邏輯或代碼中有一個簡單的明顯錯誤,我深表歉意。 就像我說的那樣,我是 Python 的新手,自從我上次編寫任何代碼並且使用 PASCAL/TSL 編寫代碼已經 30 年了:( 我將不勝感激任何幫助和建設性的批評。
我在一些 YT 視頻后解決了這個問題,哈哈。 現在唯一可以改變的是主文件中的網絡驅動程序路徑
from selenium import webdriver
import functions
driver = webdriver.Chrome(executable_path='C:\drivers\chromedriver_win32\chromedriver.exe')
functions.test_func(driver)
def test_func(driver):
import time
driver.get('https://reddit.com')
time.sleep(5)
driver.close()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.