O'Reilly logo

Python Web Scraping Cookbook by Michael Heydt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required