Search code examples
pythonseleniumtwitterfile-writing

Error trying to save scraped web data to a text file


I am new, and recently started using Python. I am trying to save the retrieved Twitter followers from web to a text file but it doesn't work.

Here's my code:

for twusernames in driver.find_elements_by_xpath('//div[@aria-label="Timeline: Followers"]//a[@role="link"]'):
    print(twusernames.get_property('href'))
    file = open('links.txt', 'w')
    file.write(twusernames.get_property('href'))
    file.close()

What am I doing wrong? :( Thanks for help.


Solution

  • This should work:

    with open('links.txt', 'w') as file
    for twusernames in driver.find_elements_by_xpath('//div[@aria-label="Timeline: Followers"]//a[@role="link"]'):
        content = twusernames.get_property('href')
        print(content)
        file.write(content)
    file.close()