Free AI web copilot to create summaries, insights and extended knowledge, download it at here
1885
Abstract
p>
<figure id="631f">
<div>
<div>
<iframe class="gist-iframe" src="/gist/terrah27/5954ed0c1818a511d9758e426a521ff0.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="f2cf">And now just to make sure it worked, lets print the most recent tweets from my stream:</p>
<figure id="a7b6">
<div>
<div>
<iframe class="gist-iframe" src="/gist/terrah27/662abf2318061d9e8f5cbeefbc655634.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><figure id="e7d7"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*wkTnIwH4_J1-o9v-KS4WgA.png"><figcaption></figcaption></figure><p id="475e">Success! We have a bunch of random tweets! But, this really isn’t very helpful, unless you’re trying to analyze the tweets of the people you follow.</p><h2 id="87fc">Get a User’s Tweets</h2><p id="c39e">Tweepy has a useful method that returns the most recent statuses posted from the specified user.</p><p id="d2ae">Let’s use this to see what Donald Trump is tweeting today:</p>
<figure id="7570">
<div>
<div>
<iframe class="gist-iframe" src="/gist/terrah27/ef7cfc156ca3f6ad3b45a7e929688711.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><figure id="8c15"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*aNQvfjxVKYG28GYBlKY1gQ.png"><figcaption></figcaption></figure><p id="8b64">Now we’re getting somewhere! We have the tweet text,
Options
but this still isn’t super useful. It just looks like a bunch of random text.</p><p id="5409">The <code>user_timeline</code> function returns a tweet object. From <a href="https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/tweet-object">Twitter’s documentation</a> we can see that each tweet object has a long list of attributes including the text attribute that we used above.</p><p id="0331">We can write a function to extract the attributes we’re interested in and create a dataframe:</p>
<figure id="8b1e">
<div>
<div>
<iframe class="gist-iframe" src="/gist/terrah27/76f9a0f0f35d6c807850525308f8a862.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><figure id="796f"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*KaDSECp6r1gw27qoblm_-A.png"><figcaption></figcaption></figure><p id="5e53">Now we’re getting somewhere. With some attributes of interest in our dataframe we can move on to analysis.</p><p id="8d7e">Some popular uses of Twitter data include topic modeling and sentiment analysis.</p><p id="40ec">Something more interesting, and perhaps with a real world application, could be to attempt to predict how many tweets Trump will post in a week.</p><figure id="9a6b"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*bDU500Gu4W4-cZPWCRbPSQ.png"><figcaption>Predictit.com market for Donald Trump tweets</figcaption></figure><h2 id="feec">Conclusion</h2><p id="23d5">While this is a great start to data mining, we’ve just barely scratched the surface. Twitter’s API has many more methods we can explore, not to mention the rest of the internet!</p><p id="ebde">I look forward to seeing what interesting data you dig up!</p></article></body>