Posted By

naveenrn on 03/14/14


Tagged

python random article wikipedia


Versions (?)

random wikipedia reader


 / Published in: Python
 

URL: http://www.reddit.com/r/beginnerprojects/comments/1jg2ru/project_random_wikipedia_article/

to open and read a random wikipedia article

  1. import urllib2
  2. import webbrowser
  3. import simplejson as json
  4.  
  5. class randomWikiArticle():
  6. curidList = []
  7.  
  8. def wikiAPI(self):
  9. random = urllib2.urlopen("http://en.wikipedia.org/w/api.php?action=query&list=random&rnnamespace=0&rnlimit=10&format=json").read()
  10. print "Select the Topic:\n"
  11. return random
  12.  
  13. def parseValue(self, parseData):
  14. global curidList
  15. curidList = []
  16. parsedData = json.loads(parseData)
  17. for key,value in parsedData['query'].iteritems():
  18. for i in range(0, 10):
  19. for k,v in value[i].iteritems():
  20. if (k == 'id'):
  21. curidList.append(v)
  22. if (k == 'title'):
  23. print i+1, v.encode('utf-8')
  24.  
  25. def openURL(self):
  26. var = input('\n')
  27. webbrowser.open_new("http://en.wikipedia.org/wiki?curid="+str(curidList[var]))
  28.  
  29. def init(self):
  30. random = randomWikiArticle().wikiAPI()
  31. randomWikiArticle().parseValue(random)
  32. randomWikiArticle().openURL()
  33.  
  34. if __name__ == '__main__':
  35. randomWikiArticle().init()

Report this snippet  

You need to login to post a comment.