Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added script for country numeric codes
- Loading branch information
Fayder Florez
committed
Sep 17, 2016
1 parent
decd80e
commit 698552e
Showing
4 changed files
with
51 additions
and
6 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# -*- coding: utf-8 -*- | ||
from bs4 import BeautifulSoup | ||
import urllib2 | ||
|
||
URL_ISO_COUNTRIES = 'https://en.wikipedia.org/wiki/ISO_3166-1' | ||
|
||
|
||
def update(countriesJSON): | ||
html = __getHTML(URL_ISO_COUNTRIES) | ||
soup = BeautifulSoup(html, "html.parser") | ||
data = __extractData(soup) | ||
return __updateJSON(countriesJSON, data) | ||
|
||
|
||
def __getHTML(url): | ||
usock = urllib2.urlopen(url) | ||
data = usock.read() | ||
usock.close() | ||
return data | ||
|
||
def __extractData(html): | ||
data = {} | ||
table = html.find('table', class_='wikitable') | ||
for row in table: | ||
nameLink = row.find('a') | ||
if nameLink != -1: | ||
countryName = nameLink['title'].encode('utf-8') | ||
for idx,element in enumerate(row): | ||
if idx == 7: | ||
numericCode = element.text | ||
data[countryName] = numericCode | ||
|
||
return data | ||
|
||
def __updateJSON(countriesJSON, data): | ||
for country in data: | ||
for countryJSON in countriesJSON: | ||
if country == countryJSON['name'].encode('utf-8'): | ||
print(country, data[country]) | ||
countryJSON['numericCode'] = data[country] | ||
|
||
return countriesJSON | ||
|
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