【发布时间】:2023-03-09 10:52:02
【问题描述】:
我正在尝试在 python 中创建一个程序,该程序将从网页获取服务器状态并将其保存到 txt 文档中。问题是每次我运行程序时,变量都会发生变化,所以它应该将每个信息都写在新的一行上。但是,它一直在同一行上运行,并且只会将以前的文本更改为新文本。感谢您的每一个回复。
from selenium import webdriver
#Creates a txt document to write to
txt = open("amx_log.txt", "w")
#Takes data and puts it into a variable
path = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(path)
driver.get("https://exampleweb.com")
serc = driver.page_source
main = driver.find_element_by_xpath("/html/body/div[1]/div[2]/div[2]/div/center[2]/table/tbody/tr[54]")
main1 = main.text
#Takes the data and writes it into txt document
txt.write(main1)
txt.write("\n")
txt.close()
driver.quit()
【问题讨论】:
-
试试
open()docs.python.org/3.8/library/functions.html#open的Python文档