get table wikipedia

Solutions on MaxInterview for get table wikipedia by the best coders in the world

showing results for - "get table wikipedia"
Edoardo
12 Jul 2018
1import requests
2from bs4 import BeautifulSoup
3
4URL = "https://en.wikipedia.org/wiki/List_of_current_heads_of_state_and_government"
5
6res = requests.get(URL).text
7soup = BeautifulSoup(res,'lxml')
8for items in soup.find('table', class_='wikitable').find_all('tr')[1::1]:
9    data = items.find_all(['th','td'])
10    try:
11        country = data[0].a.text
12        title = data[1].a.text
13        name = data[1].a.find_next_sibling().text
14    except IndexError:pass
15    print("{}|{}|{}".format(country,title,name))