-1
class Hotmail:
    def __init__(self, username, password):
        self.browser = webdriver.Chrome()
        self.username = username
        self.password = password
        self.emailsender = []
        self.emailcontent = []

    def signIn(self):
        self.browser.get("url")
        time.sleep(2)
        self.browser.maximize_window()
        username = self.browser.find_element_by_name("xxx").send_keys(self.username)
        self.browser.find_element_by_xpath("xpath").click()
        time.sleep(3)
        password = self.browser.find_element_by_name("name").send_keys(self.password)
        self.browser.find_element_by_xpath("xpath").click()        
        time.sleep(3)
        self.browser.find_element_by_name("name").click()
        self.browser.find_element_by_id("id").click()

    def getEmails(self):
        self.browser.get("anotherurl")
        time.sleep(2)

        sender = self.browser.find_elements_by_css_selector(".somecode")
        content = self.browser.find_elements_by_css_selector(".someother code")
        for i in sender:
            self.emailsender.append(i.text)
        for a in content:
            self.emailcontent.append(a.text)


hotmail = Hotmail(username, password)
hotmail.signIn()
hotmail.getEmails()
print(hotmail.emailsender)
print(hotmail.emailcontent)
# it is all ok until here, problem is below

for a,b in zip(hotmail.emailsender, hotmail.emailcontent):
    # print(a,b) this way you can see all the results
    with open("output.txt", "w") as output:
        output.write(f"{a}-----------------{b}") 

# I get only first email as result, I want all of them

As you see above I have a code that extracts my email sender names and subjects and then saves them to "output.txt" on the same path as "sender--------subject" but I get only first email, the rest cannot be inserted, anyone knows how to do it??

Edit: If you want to append it to docx file:

import docx
document = Document()
for a,b in zip(hotmail.emailsender, hotmail.emailcontent):
    document.add_paragraph(f"{a}---------------{b} \n")
document.save("yourdocfile.docx")
eyup.tatar
  • 75
  • 8

1 Answers1

1

You're constantly overwriting the same file over and over again. Everything is likely being written, but only the last written one will remain.

You'd need to either open the file in append mode (note the "a"):

for a,b in zip(hotmail.emailsender, hotmail.emailcontent):
    with open("output.txt", "a") as output:
        output.write(f"{a}-----------------{b}")

or keep the same file open for the entirety of the loop:

with open("output.txt", "w") as output:
    for a,b in zip(hotmail.emailsender, hotmail.emailcontent):
        output.write(f"{a}-----------------{b}") 

Note that with the first way, if you run this code multiple times, it will retain all the result across all runs. The second way will reset the file every time the program is run.

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
  • thank your for your comment, it was really helpful but I wonder something else. What if I want to append that data into docx file, how to do it? Do you know about it? – eyup.tatar Aug 15 '21 at 20:33
  • @KayıpRuh `docx` files will be much more complicated. `.docx` documents are actually zip files filled with formatting and other information. You would almost certainly need to use a library for that, but I don't know of any off the top of my head. If you googled for python docx libraries though, I'm sure you'd be able to find multiple. – Carcigenicate Aug 15 '21 at 20:34
  • Actually I found docx library and tried to aplly it but I got only first value. Guess I am hopeless – eyup.tatar Aug 15 '21 at 20:38
  • check the edit for docx – eyup.tatar Aug 16 '21 at 18:51