0

I'm pretty new to Python and just completed the 'automate the boring stuff with python' course. I have a script that works well for going to a site, grabbing all the necessary data I need, and printing it to my console. I'm having a problem though on how to actually save/export that data to a file. For now I'd like to be able to export it to a .txt or a .csv file. Any help is appreciated, as I can't find a straightforward answer on the web. I just need that last step to complete my project, thanks!

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
browser = webdriver.Chrome()

def getTen():

# Opens the browser and navigates to the page
browser.get('http://taxsales.lgbs.com/map?lat=29.437693458470175&lon=-98.4618145&zoom=9&offset=0&ordering=sale_date,street_name,address_full,uid&sale_type=SALE,RESALE,STRUCK%20OFF,FUTURE%20SALE&county=BEXAR%20COUNTY&state=TX&in_bbox=-99.516502,28.71637426279382,-97.407127,30.153924134433552')

# Waits until the page is loaded then clicks the accept button on the popup window
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/div/div/div[2]/button[1]"))).click()



    # Loops through 10 times, changing the listing number to correspond with 1
for i in range(1,11):

    clickable = "/html/body/ui-view/div[2]/main/aside/div[2]/property-listing[" + str(i) + "]/article/div/a"

        # Waits until the page is loaded then clicks the view more details button on the first result
    WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, clickable))).click()

# Waits until the page is loaded, then pulls all the info from the top section of the page
    info = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, "/html/body/ui-view/div/main/div/div/div[2]/property-detail-info/dl[1]")))

    # prints info to the console
    print(info.text);

    # goes back a page and repeats the process for the next listing
    browser.back()

getTen()

  • This is a very basic question wrapped in a more complicated context. `info.text` is a string (or at least can be cast to a string using `str(info.text)`) and you want to save that string to a file. The answer to your question is the same as the answer to [how to save string to file](https://stackoverflow.com/a/9536741/1483986). – 7yl4r Jun 14 '18 at 22:15

2 Answers2

0

If you are trying to save info.text, you can just open a local file and write to it. For example:

with open('output.txt', 'w') as f:
    f.write(info.text)

More info on reading and writing to files can be found here: Reading and Writing Files in Python

crookedleaf
  • 2,118
  • 4
  • 16
  • 38
-1

You can simply use you output/get text in console and use below code to get text in.txt output.txt will be your file name in which u want to save and element.text will be element or text u want to save in .txt

with open('output.txt', 'w') as f:

f.write(elements.text)