How to do it

We will start by converting the planets data into a CSV file.

  1. This will be performed using csv.  The following code writes the planets data to a CSV file (the code is in03/create_csv.py):
import csvfrom get_planet_data import get_planet_dataplanets = get_planet_data()with open('../../www/planets.csv', 'w+', newline='') as csvFile:    writer = csv.writer(csvFile)    writer.writerow(['Name', 'Mass', 'Radius', 'Description', 'MoreInfo'])for planet in planets:        writer.writerow([planet['Name'], planet['Mass'],planet['Radius'], planet['Description'], planet['MoreInfo']])
  1. The output file is put into the www folder of our project.  Examining it we see the following content::
Name,Mass,Radius,Description,MoreInfo Mercury,0.330,4879,Named ...

Get Python Web Scraping Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.