【问题标题】:Storing still changing variable in Python [duplicate]在Python中存储仍在变化的变量[重复]
【发布时间】: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()

【问题讨论】:

标签: python variables


【解决方案1】:

您正在以“w”(写入)模式打开文件。而是在此处使用“a”以附加模式打开它

txt = open("amx_log.txt", "a")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-20
    • 1970-01-01
    • 1970-01-01
    • 2017-03-29
    • 1970-01-01
    • 2012-11-24
    相关资源
    最近更新 更多