How to do it

We proceed with the recipe as follows:

  1. Execute the file as a Python script:
$ python elasticcloud_starwars.py
  1. This will loop through up to 20 characters and drop them into the sw index with a document type of people.  The code is straightforward (replace the URL with yours):
from elasticsearch import Elasticsearchimport requestsimport jsonif __name__ == '__main__':    es = Elasticsearch(        [            "https://elastic:tduhdExunhEWPjSuH73O6yLS@d7c72d3327076cc4daf5528103c46a27.us-west-2.aws.found.io:9243"        ])i = 1while i<20:    r = requests.get('http://swapi.co/api/people/' + str(i))    if r.status_code is not 200:        print("Got a " + str(r.status_code) + " so stopping")        break j = json.loads(r.content) print(i, j) #es.index(index='sw', doc_type='people', ...

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.