Skip to main content
Practice

Saving Extracted Data as a CSV File with Selenium

In the last two lessons, we learned how to use Selenium to navigate to the Yahoo Finance web page and extract market data and stock information.

In this lesson, we will learn how to output and save the extracted data into a CSV file.


Code to Save Data to a CSV File

The following code uses the io.StringIO and csv modules to handle CSV data in memory instead of using the computer's file system.

Code Example
# Create an in-memory buffer to generate CSV data
output = io.StringIO()

# Create a csv.writer object (defaults to comma-separated)
csv_writer = csv.writer(output)

# Write vertically arranged CSV data
csv_writer.writerow(["Metric", "Value"])
csv_writer.writerow(["Current Price", current_price])
csv_writer.writerow(["Previous Close", previous_close])
csv_writer.writerow(["Volume", volume])

# Convert the CSV data to a string
csv_data = output.getvalue()

# Close the in-memory buffer
output.close()

Let's break down each line step by step.

1. output = io.StringIO()

  • io.StringIO() is a class provided by Python's io module.

  • This class creates an object that operates similarly to a file but resides in memory.

  • Typically, we store CSV files on the file system, but in this case, we use StringIO() to handle data as a string in memory.

  • Here, output serves as a kind of "in-memory buffer," a temporary storage space for the CSV data we are about to write.


2. csv_writer = csv.writer(output)

  • csv.writer() is a class provided by Python's csv module.

  • csv.writer(output) converts the StringIO object output into a CSV writer object.

  • This csv_writer object provides functionality to write CSV data. By default, it uses a comma (,) to separate data fields.

  • Now, csv_writer is ready to record data in CSV format.


3. csv_writer.writerow(["Metric", "Value"])

  • csv_writer.writerow() is a function that writes a single row to the CSV file.

  • In this line, it writes the first row, containing two entries: "Metric" and "Value".

  • ["Metric", "Value"]: This list represents the names of the columns:

    • "Metric": The name of the data item (e.g., Current Price, Previous Close, Volume)
    • "Value": The value of the item (e.g., 5619.58, 5626.02, 895731735)
  • This row becomes the first line, or header row, of the CSV file.


4. csv_writer.writerow(["Current Price", current_price])

  • csv_writer.writerow() is called again to write a second row.

  • This line records the value of current_price for the item "Current Price".

  • ["Current Price", current_price]: The first item in the list is the column name "Current Price", and the second item is the actual value stored in the current_price variable.

  • This row becomes the second line of the CSV, containing the current stock price data.


5. csv_writer.writerow(["Previous Close", previous_close])

  • For the third row, it records the value of previous_close for the item "Previous Close".

  • ["Previous Close", previous_close]: "Previous Close" is the column name, and previous_close is its value.


6. csv_writer.writerow(["Volume", volume])

  • For the fourth row, it records the value of volume for the item "Volume".

  • ["Volume", volume]: "Volume" is the column name, and volume is its value.


7. csv_data = output.getvalue()

  • output.getvalue(): This method converts all the data stored in the memory buffer into a string.

  • The StringIO object output now contains the CSV data, which is retrieved as a string.

  • At this point, csv_data holds the CSV data in a string format.


8. output.close()

  • output.close(): Since the in-memory buffer is no longer needed, the StringIO object is closed.

  • This is a process of releasing memory resources, and the output object becomes unusable afterwards.


In this lesson, we have learned how to process stock data extracted with Selenium and save it as a CSV file.

In the next lesson, we will explore how to analyze monthly sales data per customer with Python and automatically send customized reports via email.

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.