Created
August 22, 2020 09:53
-
-
Save SaraM92/dc9a377afd6fdec2d2ec0d70c46ab34e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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