How to do it

Writing data to Elasticsearch is really simple. The following Python code performs this task with our planets data (03/write_to_elasticsearch.py):

from elasticsearch import Elasticsearchfrom get_planet_data import get_planet_data# create an elastic search objectes = Elasticsearch()# get the dataplanet_data = get_planet_data()for planet in planet_data:  # insert each planet into elasticsearch server  res = es.index(index='planets', doc_type='planets_info', body=planet)  print (res)

Executing this results in the following output:

{'_index': 'planets', '_type': 'planets_info', '_id': 'AV4qIF3_T0Z2t9T850q6', '_version': 1, 'result': 'created', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, 'created': True}{'_index': 'planets', ...

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.