-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwikiScrap.py
More file actions
126 lines (106 loc) · 3.68 KB
/
wikiScrap.py
File metadata and controls
126 lines (106 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 21 23:32:02 2019
@author: subhro
"""
class WebScrap:
wikiUrl = 'https://en.wikipedia.org/wiki/'
def __init__(self,country):
from bs4 import BeautifulSoup as bs
import requests
self.myUrl = WebScrap.wikiUrl + country
self.source = requests.get(self.myUrl).content
self.soup = bs(self.source,'lxml')
def perCapitaIncome(self):
'''
scraps the per capita income(nominal) of the country from wikipedia
'''
import numpy as np
import re
self.incomeRec = self.soup.find_all('tr',class_ = 'mergedbottomrow')
nPerCapita = ''
for i in self.incomeRec:
if 'per capita' in str(i).lower():
nPerCapita = i.td.text.split('$')[1].replace('[',' ').replace(',','')
try:
if nPerCapita > '':
return float(re.findall('\d+',nPerCapita)[0])
else:
return np.NaN
except ValueError:
print('URL: ',self.myUrl)
print('Error1 in Per capita Income: ',nPerCapita)
except IndexError:
print('URL: ',self.myUrl)
print('Error2 in Per capita Income: ',nPerCapita)
def population(self):
'''
scraps the latest available population of the country from wikipedia
'''
import numpy as np
self.P = self.soup.find_all('tr',class_ = 'mergedrow')
cont = True
pop = ''
for p in self.P:
if 'population' in str(p).lower() and cont:
pop = p.td.text.replace('[',' ').replace('(',' ')
cont = False
try:
if pop > '':
return int(pop.split()[0].replace(',',''))
#return int(re.findall('\d+\,\d+\,\d+\,\d+',pop)[0])
else:
return np.NaN
except ValueError:
print('URL: ',self.myUrl)
print('Error1 in Population: ',pop)
except IndexError:
print('URL: ',self.myUrl)
print('Error2 in Population: ',pop)
def giniCoefficient(self):
'''
scraps the Gini Coefficient of the country from wikipedia
'''
import numpy as np
import re
self.G = self.soup.find_all('tr')
gini = ''
cont = True
for g in self.G:
if 'gini coefficient' in str(g).lower() and cont:
gini = g.td.text
cont = False
try:
if gini > '':
return float(re.findall('\d+\.\d+',gini)[0])
else:
return np.NaN
except ValueError:
print('URL: ',self.myUrl)
print('Error1 in Gini: ',gini)
except IndexError:
return float(re.findall('\d+',gini)[0])
def humanDevIndex(self):
'''
scraps the HDI of the country from wikipedia
'''
import numpy as np
import re
self.HDI = self.soup.find_all('tr')
hdi = ''
cont = True
for h in self.HDI:
if 'human development index' in str(h).lower() and cont:
hdi = h.td.text
cont = False
try:
if hdi > '':
return float(re.findall('\d+\.\d+',hdi)[0])
else:
return np.NaN
except ValueError:
print('URL: ',self.myUrl)
print('Error1 in HDI: ',hdi)
except IndexError:
return np.NaN