Python 在.txt文档中写入单词,不要重复
因此,我尝试在网站中挑选单词,并用python将它们写在Python 在.txt文档中写入单词,不要重复,python,selenium,Python,Selenium,因此,我尝试在网站中挑选单词,并用python将它们写在.txt文档中。每次出现新单词时,我都需要这样做,但这不是问题所在。问题是每次我运行代码时,.txt都会重复相同的单词 这是我的密码: from selenium import webdriver import time options = webdriver.ChromeOptions() options.add_argument('--profile-directory=Default') options.add_argument('
.txt
文档中。每次出现新单词时,我都需要这样做,但这不是问题所在。问题是每次我运行代码时,.txt
都会重复相同的单词
这是我的密码:
from selenium import webdriver
import time
options = webdriver.ChromeOptions()
options.add_argument('--profile-directory=Default')
options.add_argument('--user-data-dir=D:/Users/daichi/Desktop/Bot/chrome')
url = 'https://web.whatsapp.com/'
driver = webdriver.Chrome('/Users/daichi/Desktop/Bot/chromedriver/chromedriver', options = options)
words = open("words.txt","r+")
driver.get(url)
time.sleep(10)
user = driver.find_element_by_xpath('//span[@title="A"]')
user.click()
time.sleep(2)
messages = driver.find_elements_by_xpath('//*[text()[contains(.,"!web")]]')
for message in messages:
if message.text in words.read():
print("this word is alrealy on the .txt!")
elif message.text not in words.read():
words.write(message.text + "\n")
对不起,我英语不好,你好,来自巴西。你总是在读同一份文件
twetts = ['new 1','new 2','new 2']
for twett in twetts:
if twett in open('palavras.txt','r').read():
print("the word is alrealy in the txt")
else:
wf = open('palavras.txt','a')
wf.write(f"{twett}\n")
wf.close()
您可能对未来感兴趣。请提供预期的。显示中间结果与您预期的不同之处。我们应该能够复制和粘贴一个连续的代码块,执行该文件,并再现您的问题以及跟踪问题点的输出。您发布的代码在未定义的符号和文件上失败。您的一般问题描述不清楚:为什么程序在再次运行时不执行相同的操作?什么是
palavras
?档案?那是怎样格式化的?