{"id":18,"date":"2016-03-31T02:20:08","date_gmt":"2016-03-31T02:20:08","guid":{"rendered":"http:\/\/intelligentonlinetools.com\/blog\/?p=18"},"modified":"2016-04-07T18:17:08","modified_gmt":"2016-04-07T18:17:08","slug":"data-mining-twitter-with-python","status":"publish","type":"post","link":"http:\/\/intelligentonlinetools.com\/blog\/2016\/03\/31\/data-mining-twitter-with-python\/","title":{"rendered":"Data Mining Twitter Data with Python"},"content":{"rendered":"<p>Twitter is an online social networking service that enables users to send and read short 140-character messages called &#8220;tweets&#8221;. [1]<br \/>\nTwitter users are tweeting about different topics based on their interests and goals.<br \/>\nA word, phrase or topic that is mentioned at a greater rate than others is said to be a &#8220;trending topic&#8221;. Trending topics become popular either through a concerted effort by users, or because of an event that prompts people to talk about a specific topic. [1]<br \/>\nThere is wide interest in analyzing of trending data from Twitter.<br \/>\nAnd in this post we will look at searching and downloading the tweets related to specific hashtag. We will use Python and Twitter API. Our example will be search tweets related to &#8220;deep learning&#8221;. After downloading Twitter data we will also look at some data manipulations with the data.<\/p>\n<p>The example of downloading of Twitter data is based on the work [2]<br \/>\nBelow is source code:<\/p>\n<p>import twitter<br \/>\nimport json<br \/>\nCONSUMER_KEY =&#8221;xxxxxx&#8221;<br \/>\nCONSUMER_SECRET =&#8221;xxxxxx&#8221;<br \/>\nOAUTH_TOKEN = &#8220;xxxxxx&#8221;<br \/>\nOAUTH_TOKEN_SECRET = &#8220;xxxxxx&#8221;<br \/>\nauth = twitter.oauth.OAuth (OAUTH_TOKEN, OAUTH_TOKEN_SECRET, CONSUMER_KEY, CONSUMER_SECRET)<br \/>\ntwitter_api= twitter.Twitter(auth=auth)<br \/>\nq=&#8217;#deep learning&#8217;<br \/>\ncount=100<br \/>\nsearch_results = twitter_api.search.tweets (q=q, count=count)<br \/>\nstatuses=search_results[&#8216;statuses&#8217;]<br \/>\nfor _ in range(5):<br \/>\n   &nbsp;&nbsp;&nbsp;&nbsp;print &#8220;Length of statuses&#8221;, len(statuses)<br \/>\n   &nbsp;&nbsp;&nbsp;&nbsp;try:<br \/>\n        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;next_results=search_results[&#8216;search_metadata&#8217;][&#8216;next_results&#8217;]<br \/>\n   &nbsp;&nbsp;&nbsp;&nbsp;except KeyError, e: #result does not exist<br \/>\n      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break<br \/>\n   &nbsp;&nbsp;&nbsp;&nbsp;kwargs=dict( [kv.split(&#8216;=&#8217;) for kv in next_results[1:].split(&#8220;&#038;&#8221;)])<br \/>\n   &nbsp;&nbsp;&nbsp;&nbsp;search_results = twitter_api.search.tweets(**kwargs)<br \/>\n   &nbsp;&nbsp;&nbsp;&nbsp;statuses += search_results[&#8216;statuses&#8217;]<br \/>\n# Show one sample search result by slicing the list<br \/>\nprint json.dumps(statuses[0], indent=10)<br \/>\nhashtags = [ hashtag[&#8216;text&#8217;]<br \/>\n    &nbsp;&nbsp;&nbsp;&nbsp;for status in statuses<br \/>\n       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for hashtag in status[&#8216;entities&#8217;][&#8216;hashtags&#8217;] ]<br \/>\nurls = [ urls[&#8216;url&#8217;]<br \/>\n    &nbsp;&nbsp;&nbsp;&nbsp;for status in statuses<br \/>\n       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for urls in status[&#8216;entities&#8217;][&#8216;urls&#8217;] ]<br \/>\ntexts = [ status[&#8216;text&#8217;]<br \/>\n    &nbsp;&nbsp;&nbsp;&nbsp;for status in statuses<br \/>\n       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ]<br \/>\n#Created_at is date time when created<br \/>\ncreated_ats = [ status[&#8216;created_at&#8217;]<br \/>\n    &nbsp;&nbsp;&nbsp;&nbsp;for status in statuses<br \/>\n        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]<br \/>\nprint json.dumps(hashtags[0:50], indent=1)<br \/>\nprint json.dumps(urls[0:50], indent=1)<br \/>\nprint json.dumps(texts[0:50], indent=1)<br \/>\nprint json.dumps(created_ats[0:50], indent=1)<br \/>\n# Now we append some data into the file<br \/>\nwith open(&#8220;data.txt&#8221;, &#8220;a&#8221;) as myfile:<br \/>\n     &nbsp;&nbsp;&nbsp;&nbsp;for w in hashtags:           &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myfile.write(w)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myfile.write(&#8220;\\n&#8221;)<br \/>\n# count of word frequencies<br \/>\nwordcounts = {}<br \/>\nfor term in hashtags:<br \/>\n    &nbsp;&nbsp;&nbsp;&nbsp;wordcounts[term] = wordcounts.get(term, 0) + 1<br \/>\nitems = [(v, k) for k, v in wordcounts.items()]<br \/>\nfor count, word in sorted(items, reverse=True):<br \/>\n    &nbsp;&nbsp;&nbsp;&nbsp;print(&#8220;%5d %s&#8221; % (count, word))<br \/>\n# in case we need extract date or month or year<br \/>\nfor x in created_ats:<br \/>\n   &nbsp;&nbsp;&nbsp;&nbsp;print x<br \/>\n   &nbsp;&nbsp;&nbsp;&nbsp;print x[4:10]<br \/>\n   &nbsp;&nbsp;&nbsp;&nbsp;print x[26:31]<br \/>\n   &nbsp;&nbsp;&nbsp;&nbsp;print x[4:7]<\/p>\n<p>Output example for last for loop (just one cycle)<br \/>\nWed Mar 30 02:10:20 +0000 2016<br \/>\nMar 30<br \/>\n2016<br \/>\nMar<\/p>\n<p>Any comments or suggestions are welcome.<\/p>\n<p><b>References<\/b><br \/>\n[1] https:\/\/en.wikipedia.org\/wiki\/Twitter Twitter, From Wikipedia<br \/>\n[2] http:\/\/www-scf.usc.edu\/~aupadhya\/Mining.pdf MINING DATA FROM<br \/>\nTWITTER by Abhishanga Upadhyay, Luis Mao, Malavika Goda Krishna<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Twitter is an online social networking service that enables users to send and read short 140-character messages called &#8220;tweets&#8221;. [1] Twitter users are tweeting about different topics based on their interests and goals. A word, phrase or topic that is mentioned at a greater rate than others is said to be a &#8220;trending topic&#8221;. Trending &#8230; <a title=\"Data Mining Twitter Data with Python\" class=\"read-more\" href=\"http:\/\/intelligentonlinetools.com\/blog\/2016\/03\/31\/data-mining-twitter-with-python\/\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":[]},"categories":[2],"tags":[],"jetpack_publicize_connections":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Data Mining Twitter Data with Python - Machine Learning Applications<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/intelligentonlinetools.com\/blog\/2016\/03\/31\/data-mining-twitter-with-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Data Mining Twitter Data with Python - Machine Learning Applications\" \/>\n<meta property=\"og:description\" content=\"Twitter is an online social networking service that enables users to send and read short 140-character messages called &#8220;tweets&#8221;. [1] Twitter users are tweeting about different topics based on their interests and goals. A word, phrase or topic that is mentioned at a greater rate than others is said to be a &#8220;trending topic&#8221;. Trending ... Read more\" \/>\n<meta property=\"og:url\" content=\"http:\/\/intelligentonlinetools.com\/blog\/2016\/03\/31\/data-mining-twitter-with-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Machine Learning Applications\" \/>\n<meta property=\"article:published_time\" content=\"2016-03-31T02:20:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2016-04-07T18:17:08+00:00\" \/>\n<meta name=\"author\" content=\"owygs156\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"owygs156\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"http:\/\/intelligentonlinetools.com\/blog\/2016\/03\/31\/data-mining-twitter-with-python\/\",\"url\":\"http:\/\/intelligentonlinetools.com\/blog\/2016\/03\/31\/data-mining-twitter-with-python\/\",\"name\":\"Data Mining Twitter Data with Python - Machine Learning Applications\",\"isPartOf\":{\"@id\":\"http:\/\/intelligentonlinetools.com\/blog\/#website\"},\"datePublished\":\"2016-03-31T02:20:08+00:00\",\"dateModified\":\"2016-04-07T18:17:08+00:00\",\"author\":{\"@id\":\"http:\/\/intelligentonlinetools.com\/blog\/#\/schema\/person\/7a886dc5eb9758369af2f6d2cb342478\"},\"breadcrumb\":{\"@id\":\"http:\/\/intelligentonlinetools.com\/blog\/2016\/03\/31\/data-mining-twitter-with-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/intelligentonlinetools.com\/blog\/2016\/03\/31\/data-mining-twitter-with-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/intelligentonlinetools.com\/blog\/2016\/03\/31\/data-mining-twitter-with-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/intelligentonlinetools.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Data Mining Twitter Data with Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/intelligentonlinetools.com\/blog\/#website\",\"url\":\"http:\/\/intelligentonlinetools.com\/blog\/\",\"name\":\"Machine Learning Applications\",\"description\":\"Artificial intelligence, data mining and machine learning for building web based tools and services.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/intelligentonlinetools.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"http:\/\/intelligentonlinetools.com\/blog\/#\/schema\/person\/7a886dc5eb9758369af2f6d2cb342478\",\"name\":\"owygs156\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/intelligentonlinetools.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/2.gravatar.com\/avatar\/b351def598609cb4c0b5bca26497c7e5?s=96&d=mm&r=g\",\"contentUrl\":\"http:\/\/2.gravatar.com\/avatar\/b351def598609cb4c0b5bca26497c7e5?s=96&d=mm&r=g\",\"caption\":\"owygs156\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Data Mining Twitter Data with Python - Machine Learning Applications","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/intelligentonlinetools.com\/blog\/2016\/03\/31\/data-mining-twitter-with-python\/","og_locale":"en_US","og_type":"article","og_title":"Data Mining Twitter Data with Python - Machine Learning Applications","og_description":"Twitter is an online social networking service that enables users to send and read short 140-character messages called &#8220;tweets&#8221;. [1] Twitter users are tweeting about different topics based on their interests and goals. A word, phrase or topic that is mentioned at a greater rate than others is said to be a &#8220;trending topic&#8221;. Trending ... Read more","og_url":"http:\/\/intelligentonlinetools.com\/blog\/2016\/03\/31\/data-mining-twitter-with-python\/","og_site_name":"Machine Learning Applications","article_published_time":"2016-03-31T02:20:08+00:00","article_modified_time":"2016-04-07T18:17:08+00:00","author":"owygs156","twitter_card":"summary_large_image","twitter_misc":{"Written by":"owygs156","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"http:\/\/intelligentonlinetools.com\/blog\/2016\/03\/31\/data-mining-twitter-with-python\/","url":"http:\/\/intelligentonlinetools.com\/blog\/2016\/03\/31\/data-mining-twitter-with-python\/","name":"Data Mining Twitter Data with Python - Machine Learning Applications","isPartOf":{"@id":"http:\/\/intelligentonlinetools.com\/blog\/#website"},"datePublished":"2016-03-31T02:20:08+00:00","dateModified":"2016-04-07T18:17:08+00:00","author":{"@id":"http:\/\/intelligentonlinetools.com\/blog\/#\/schema\/person\/7a886dc5eb9758369af2f6d2cb342478"},"breadcrumb":{"@id":"http:\/\/intelligentonlinetools.com\/blog\/2016\/03\/31\/data-mining-twitter-with-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/intelligentonlinetools.com\/blog\/2016\/03\/31\/data-mining-twitter-with-python\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/intelligentonlinetools.com\/blog\/2016\/03\/31\/data-mining-twitter-with-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/intelligentonlinetools.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Data Mining Twitter Data with Python"}]},{"@type":"WebSite","@id":"http:\/\/intelligentonlinetools.com\/blog\/#website","url":"http:\/\/intelligentonlinetools.com\/blog\/","name":"Machine Learning Applications","description":"Artificial intelligence, data mining and machine learning for building web based tools and services.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/intelligentonlinetools.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"http:\/\/intelligentonlinetools.com\/blog\/#\/schema\/person\/7a886dc5eb9758369af2f6d2cb342478","name":"owygs156","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/intelligentonlinetools.com\/blog\/#\/schema\/person\/image\/","url":"http:\/\/2.gravatar.com\/avatar\/b351def598609cb4c0b5bca26497c7e5?s=96&d=mm&r=g","contentUrl":"http:\/\/2.gravatar.com\/avatar\/b351def598609cb4c0b5bca26497c7e5?s=96&d=mm&r=g","caption":"owygs156"}}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p7h1IJ-i","jetpack-related-posts":[{"id":1446,"url":"http:\/\/intelligentonlinetools.com\/blog\/2017\/11\/06\/10-new-top-resources-on-machine-learning-from-around-the-web\/","url_meta":{"origin":18,"position":0},"title":"10 New Top Resources on Machine Learning from Around the Web","date":"November 6, 2017","format":false,"excerpt":"For this post I put new and most interesting machine learning resources that I recently found on the web. This is the list of useful resources in such areas like stock market forecasting, text mining, deep learning, neural networks and getting data from Twitter. Hope you enjoy the reading. 1.\u2026","rel":"","context":"In &quot;Machine Learning&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":227,"url":"http:\/\/intelligentonlinetools.com\/blog\/2016\/05\/28\/using-python-for-mining-data-from-twitter\/","url_meta":{"origin":18,"position":1},"title":"Using Python for Mining Data From Twitter","date":"May 28, 2016","format":false,"excerpt":"Twitter is increasingly being used for business or personal purposes. With Twitter API there is also an opportunity to do data mining of data (tweets) and find interesting information. In this post we will take a look how to get data from Twitter, prepare data for analysis and then do\u2026","rel":"","context":"In &quot;Artificial Intelligence&quot;","img":{"alt_text":"Frequency of Hashtags","src":"https:\/\/i0.wp.com\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2016\/05\/Frequency-of-Hashtags-300x171.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":827,"url":"http:\/\/intelligentonlinetools.com\/blog\/2017\/01\/11\/apis\/","url_meta":{"origin":18,"position":2},"title":"Useful APIs for Your Web Site","date":"January 11, 2017","format":false,"excerpt":"Here\u2019s a useful list of resources on how to create an API, compiled from posts that were published recently on this blog. The included APIs can provide a fantastic ways to enhance websites. 1. The WordPress(WP) API exposes a simple yet powerful interface to WP Query, the posts API, post\u2026","rel":"","context":"In &quot;API Programming&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":133,"url":"http:\/\/intelligentonlinetools.com\/blog\/2016\/03\/11\/133\/","url_meta":{"origin":18,"position":3},"title":"7 Ideas for Building Text Mining Application","date":"March 11, 2016","format":false,"excerpt":"It is no doubt that the web is growing at an incredible pace. And as the most documents of the web consist of the text, the applications of text analytics or text mining are getting more use. In such applications the textual data are used for extracting intelligence from a\u2026","rel":"","context":"In &quot;Data Mining&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":256,"url":"http:\/\/intelligentonlinetools.com\/blog\/2016\/06\/05\/using-python-for-mining-data-from-twitter-visualization-and-other-enchancements\/","url_meta":{"origin":18,"position":4},"title":"Using Python for Data Visualization of Clustering Results","date":"June 5, 2016","format":false,"excerpt":"In one of the previous post http:\/\/intelligentonlinetools.com\/blog\/2016\/05\/28\/using-python-for-mining-data-from-twitter\/ python source code for mining Twitter data was implemented. Clustering was applied to put tweets in different groups using bag of words representation for the text. The results of clustering were obtained via numerical matrix. Now we will look at visualization of clustering\u2026","rel":"","context":"In &quot;Artificial Intelligence&quot;","img":{"alt_text":"Data Visualization for Clustering Results","src":"https:\/\/i0.wp.com\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2016\/06\/data-visualization1-300x220.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":510,"url":"http:\/\/intelligentonlinetools.com\/blog\/2016\/08\/19\/getting-data-from-wikipedia-using-python\/","url_meta":{"origin":18,"position":5},"title":"Getting Data From Wikipedia Using Python","date":"August 19, 2016","format":false,"excerpt":"Recently I come across python package Wikipedia which is a Python library that makes it easy to access and parse data from Wikipedia. Using this library you can search Wikipedia, get article summaries, get data like links and images from a page, and more. Wikipedia wraps the MediaWiki API so\u2026","rel":"","context":"In &quot;API Programming&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/posts\/18"}],"collection":[{"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/comments?post=18"}],"version-history":[{"count":24,"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/posts\/18\/revisions"}],"predecessor-version":[{"id":195,"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/posts\/18\/revisions\/195"}],"wp:attachment":[{"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/media?parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/categories?post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/tags?post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}