twitter-3-whats-trending-multiple-locations

Published by Darron Toy on


What’s trending in multiple locations.
Now let’s add another location to our trending places. Here we’re adding
London. It’s ID is four four four one eight so let me save and run this code what
we’re going to see is all the tweets for Dublin followed by all the tweets for
London and then you can look through that JSON output and see if there are
any common trends between both locations so you can see here all the tweets are
listed just in chronological order so it’s not straightforward to work out
where the same topic has been trending in both cities. So there’s actually an
easier way to do it and that’s to use python’s set data structure to do the
work for you. A setwise intersection computes common items between sets and a
setwise Union combines all of the items from sets and the set wise difference
among sets acts as a subtraction operation in which items from one set
are removed from another. What we’re going to do here is find the
intersection of the two result sets what we’re using here is actually a list
comprehension which is quite a nifty part of the Python tool set. What this
does is it builds the result sets into lists for examination.
So we’re looping through the table underscore trends results and extracting
the name attribute for each result and then adding it to a set which is
assigned to the dub underscore trends underscore set variable and then we do
exactly the same thing for London now we find the common trends to both
sets by using the set intersection and now we just print that out. You
may sometimes get an empty set printed out and that’s simply because there happens to be no common trend between the locations at that given moment. So you
may have to retry several times and see the data change so we have one thing
trending on both locations. So in this lesson you learned how to create a
Twitter application and access it via the Twitter API.
You used tweepy to connect to and access data from the API. You also learned how
to get lists of trending topics from different WOEID locations and JSON
format the output. Finally you finished up by comparing two sets a result data
to see if there are any overlapping trends. Now try the challenges

Categories: ArticlesBlog

Leave a Reply

Your email address will not be published. Required fields are marked *