The program should accept a name of a country as input. It should then parse the Wikipedia page for that country and find all links to the wikipedia pages of other countries on that page and make a list of all such countries.
For example, if the argument was India and http://en.wikipedia.org/wiki/India has links to wiki pages of Nepal and Pakistan, the list associated with India is [Nepal, Pakistan]. The program should recursively perform the above operation for any country you have not encountered so far.
After parsing the wiki page for India, it should parse the wiki page of Nepal, Pakistan and so on.
The final output is a json object with key being the country and value being the list of countries which were linked to the wiki page for the key country.
Sample output can be:
{"India": ["Nepal", "Pakistan"], "Nepal": ["India", "China"], "Pakistan": ["India"], "China": ["Japan"], "Japan": ["India", "China"]}
This is what I have tried, but I want to get the best approach for it:
import pycountry
import wikipedia
country_dict = {}
final_dict = {}
refr_list = []
def GetCountryDict():
country_list = list(pycountry.countries)
for country in country_list:
country_dict[country.name] = 1
return country_dict
def internalParse(internal_list,final_dict,countryDict,refr_list):
for i in refr_list:
if not final_dict.has_key(i):
WikiParser(i,countryDict)
else:
refr_list.remove(i)
def WikiParser(country_name,country_dict):
wiki_resp =[]
internal_list = []
try:
wiki_resp = wikipedia.page(country_name)
except:
pass
for i in wiki_resp.links:
if country_dict.has_key(i):
if i not in internal_list:
refr_list.append(i)
internal_list.append(i)
final_dict[country_name] = internal_list
try:
refr_list.remove(country_name)
except:
pass
return internal_list, final_dict,refr_list
if __name__ == '__main__':
country_name = raw_input("Please enter country name: ")
countryDict = GetCountryDict()
internal_list,final_dict,refr_list = WikiParser(country_name,countryDict)
while refr_list:
internalParse(internal_list,final_dict,countryDict,refr_list)