Python 100 project #3: Trending Wordcloud

Small code demo to try google trend api module(unofficial) and wordcloud module.

It generates wordcloud based on the trending news of the day and its title. The size of the word in the word cloud stands for its hotness (=how much it has been searched).

The module I used:

from pytrends.request import TrendReq
from wordcloud import WordCloud

# Login to Google. Only need to run this once, the rest of requests will use the same session.
pytrend = TrendReq()

trending_searches_df = pytrend.trending_searches()

# TODO: (shogo) needs some normalization so that the resulting cloud gets better visibility

trending_dict = {}

for id, row in trending_searches_df.iterrows():
    trending_dict[row.title] = row.trafficBucketLowerBound

# Generate a word cloud image
wordcloud = WordCloud()
wordcloud.generate_from_frequencies(frequencies=trending_dict)

# The pil way to save image
wordcloud.to_file("trending.png")

 

Resulting Image (as of 26th Apr. 2018):