所以我试图在这里做一些事情 - 我显然一点经验都没有 - 我从维基百科表格中读取数据,然后将其基本绘制在条形图上。
这非常混乱,但我本质上想做的是制作一个包含两个变量的字典,然后将其用作我的“数据帧”进行可视化。
source = requests.get('https://en.wikipedia.org/wiki/List_of_countries_by_alcohol_consumption_per_capita').text
soup = BeautifulSoup(source, 'lxml')
body = soup.find('body')
table = body.find('table')
tablemain = table.find('table', class_ ='wikitable nowrap sortable mw-datatable')
countrylist = []
for data in tablemain.find_all('tbody'):
rows = data.find_all('tr')
rows.pop(0)
for row in rows:
country = row.find('a')
countrylist.append(country)
countrynames = []
for x in countrylist:
names = x.get('title')
countrynames.append(names)
total = []
for data in tablemain.find_all('tbody'):
rows = data.find_all('tr')
rows.pop(0)
for row in rows:
tot = row.find_all('td') [1]
total.append(tot.text)
floatal = [] …Run Code Online (Sandbox Code Playgroud)