Python to Wikipedia Search and Generate a Wordcloud Image with Full Source Code For Beginners

Wordcloud Images for Wikipedia Article

  • Python script that prompts the user for input, searches for the corresponding article on Wikipedia and generates a wordcloud based on the searched article.


  • pip install the models in requirements.txt from your command prompt.


  • beautifulsoup4==4.9.1
  • certifi==2020.6.20
  • chardet==3.0.4
  • cycler==0.10.0
  • idna==2.10
  • kiwisolver==1.2.0
  • matplotlib==3.3.1
  • numpy==1.19.1
  • Pillow==7.2.0
  • pyparsing==2.4.7
  • python-dateutil==2.8.1
  • requests==2.24.0
  • six==1.15.0
  • soupsieve==2.0.1
  • urllib3==1.25.10
  • wikipedia==1.4.0
  • wordcloud==1.8.0

Source Code:

from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator import matplotlib.pyplot as plt import wikipedia import sys import warnings # supressing unnecessary warnings warnings.filterwarnings("ignore") # function to search the wikipedia article and generate the wordcloud def gen_cloud(topic): try: content = str( except: print("Error, try searching something else...") sys.exit() STOPWORDS.add('==') stopwords = set(STOPWORDS) wordcloud = WordCloud(stopwords=stopwords, max_words=200, background_color="black", width=600, height=350).generate(content) return wordcloud # function to save the wordcloud to current directory def save_cloud(wordcloud): wordcloud.to_file("./wordcloud.png") # function to display the wordcloud with matplotlib def show_cloud(wordcloud): plt.imshow(wordcloud, interpolation='bilinear') plt.axis("off") # driver code if __name__ == '__main__': topic = input("What do you want to search: ").strip() wordcloud = gen_cloud(topic) save_cloud(wordcloud) print("Wordcloud saved to current directory as wordcloud.png") desc = input("Do you wish to see the output(y/n): ") if desc == 'y': show_cloud(wordcloud) sys.exit()
Code language: PHP (php)


Leave a Comment