Skip to content

Instantly share code, notes, and snippets.

@SaraM92
Created August 22, 2020 09:53
Show Gist options
  • Save SaraM92/dc9a377afd6fdec2d2ec0d70c46ab34e to your computer and use it in GitHub Desktop.
Save SaraM92/dc9a377afd6fdec2d2ec0d70c46ab34e to your computer and use it in GitHub Desktop.
#Import needed libraries
from bs4 import BeautifulSoup as bs
import requests as rq
import pygal
from IPython.display import display, HTML
#Fetch HTML
url = 'https://en.wikipedia.org/wiki/List_of_countries_by_coffee_production'
#Extract HTMl tree
page = rq.get(url).text
soup = bs(page)
#Find countries and quantiy
table = soup.find('table')
top_10_countries = []
for row in table.find_all('tr')[2:11]:
temp = row.text.replace('\n\n',' ').strip() #obtain only the quantiy in tons
temp_list = temp.split()
top_10_countries.append((temp_list[0],temp_list[2]))
#Plot the top 10 countries
bar_chart = pygal.Bar(height=400)
[bar_chart.add(item[0],int(item[1].replace(',',''))) for item in top_10_countries]
display(HTML(base_html.format(rendered_chart=bar_chart.render(is_unicode=True))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment