Skip to content

Instantly share code, notes, and snippets.

@gvgvgvijayan
Created October 16, 2019 04:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gvgvgvijayan/ff238e3a809b3cbb942e451c83da23cc to your computer and use it in GitHub Desktop.
Save gvgvgvijayan/ff238e3a809b3cbb942e451c83da23cc to your computer and use it in GitHub Desktop.
import requests
import openpyxl
import time
#CONSTANTS
API_URL = 'https://restcountries.eu/rest/v2/name/'
def extract_country_details(search_term):
parameters = {
'fullText': 'true',
'fields': 'capital;region;population'
}
response = requests.get(API_URL + search_term, params=parameters)
json = response.json()
if response.status_code != 200:
return 'error'
return json[0]
def write_country_details():
filename = 'top5_populated_countries.xlsx'
wb = openpyxl.load_workbook(filename)
ws = wb.active
wc = ws['A']
i = 1
for cell in wc[1:]:
i += 1
country_details = extract_country_details(cell.value)
time.sleep(10)
ws['B' + str(i)] = country_details['capital']
ws['C' + str(i)] = country_details['region']
ws['D' + str(i)] = country_details['population']
wb.save(filename)
write_country_details()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment