{"id":5033,"date":"2013-04-22T05:05:14","date_gmt":"2013-04-22T05:05:14","guid":{"rendered":"https:\/\/www.pythonforbeginners.com\/?p=5033"},"modified":"2020-08-28T15:52:21","modified_gmt":"2020-08-28T20:52:21","slug":"tweet-search-with-python","status":"publish","type":"post","link":"https:\/\/www.pythonforbeginners.com\/python-on-the-web\/tweet-search-with-python","title":{"rendered":"Tweet Search with Python"},"content":{"rendered":"\n<h2>Overview<\/h2>\n<p>Twitter&#8217;s API is REST-based and will return results as either XML or JSON, as well as both RSS and ATOM feed formats. Public timelines can be accessed by any client, but all other Twitter methods require authentication.<\/p>\n<h2>About this script<\/h2>\n<p>The program is well documented and should be straightforward. Open up a text editor, copy &amp; paste the code below.<\/p>\n<p>Save the file as: &#8220;tweet_search.py&#8221; and exit the editor.<\/p>\n<h2>Getting Started<\/h2>\n<p>Let&#8217;s take a look at the program below that we call tweet_search.py<\/p>\n<pre><code class=\"language-python\">#!\/usr\/bin\/python\n\nimport json\nimport sys\nimport urllib2\nimport os\n\nusage = \"\"\"\nUsage: .\/tweet_search.py 'keyword'\ne.g .\/tweet_search.py pythonforbeginners\n\nUse \"+\" to replace whitespace\"\ne.g .\/tweet_search.py \"python+for+beginners\"\n\"\"\"\n\n# Check that the user puts in an argument, else print the usage variable, then quit.\nif len(sys.argv)!=2:\n    print (usage)\n    sys.exit(0)\n\n# The screen name in Twitter, is the screen name of the user for whom to return results for. \n\n# Set the screen name to the second argument\nscreen = sys.argv[1]\n\n# Open the twitter search URL the result will be shown in json format\nurl = urllib2.urlopen(\"http:\/\/search.twitter.com\/search.json?q=\"+screen)\n\n#convert the data and load it into json\ndata = json.load(url)\n\n#to print out how many tweets there are\nprint len(data), \"tweets\"\n\n# Start parse the tweets from the result\n\n# Get only text\nfor tweet in data[\"results\"]:\n    print tweet[\"text\"]\n\n# Get the status and print out the contents\nfor status in data['results']:\n    print \"(%s) %s\" % (status[\"created_at\"], status[\"text\"])\n<\/code><\/pre>\n<h2>How does it work?<\/h2>\n<p>Let&#8217;s break down the script to see what it does.<\/p>\n<p>The script starts with importing the modules we are going to need<\/p>\n<p>Line 3-6<\/p>\n<pre><code class=\"language-python\">import json\nimport sys\nimport urllib2\nimport os\n<\/code><\/pre>\n<p>We create a usage variable to explain how to use the script.<\/p>\n<p><code class=\"language-python\"><strong>Line 8-14<\/strong>\n\nusage = \"\"\"\nUsage: .\/tweet_search.py 'keyword'\ne.g .\/tweet_search.py pythonforbeginners\n \nUse \"+\" to replace whitespace\"\ne.g .\/tweet_search.py \"python+for+beginners\"\n\"\"\"\n<\/code>\n<\/p><p>On Line 16 we check that the user puts in an argument, else print the usage variable, then quit.<\/p>\n<pre><code class=\"language-python\">\n\nif len(sys.argv)!=2:\n    print (usage)\n    sys.exit(0)\n<\/code><\/pre>\n<p>Line 21-24 sets the Twitter screen name to the second argument.<\/p>\n<pre><code class=\"language-python\">\n\nscreen = sys.argv[1]\n<\/code><\/pre>\n<p>Line 27 open the twitter search URL and the result will be shown in json format.<\/p>\n<pre><code class=\"language-python\">\n\nurl = urllib2.urlopen(\"http:\/\/search.twitter.com\/search.json?q=\"+screen)\n<\/code><\/pre>\n<p>Line 30 converts the data and loads it into json<\/p>\n<pre><code class=\"language-python\">\n\ndata = json.load(url)\n<\/code><\/pre>\n<p>On Line 33 we print out the number of tweets<\/p>\n<pre><code class=\"language-python\">\n\nprint len(data), \"tweets\"\n<\/code><\/pre>\n<p>From Line 38 we start to parse the tweets from the result<\/p>\n<pre><code class=\"language-python\">for tweet in data[\"results\"]:\n    print tweet[\"text\"]\n<\/code><\/pre>\n<p>The last thing we do in this script is to get the status and print out the contents (Line 42)<\/p>\n<pre><code class=\"language-python\">\n\nfor status in data['results']:\n    print \"(%s) %s\" % (status[\"created_at\"], status[\"text\"])\n<\/code><\/pre>\n<p>Go through the script line by line to see what it does. Make sure to look at it, and try to understand it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overview Twitter&#8217;s API is REST-based and will return results as either XML or JSON, as well as both RSS and ATOM feed formats. Public timelines can be accessed by any client, but all other Twitter methods require authentication. About this script The program is well documented and should be straightforward. Open up a text editor, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[9,172,94],"tags":[],"class_list":{"0":"post-5033","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-code-snippets-source-code","7":"category-commandline","8":"category-python-on-the-web","9":"entry"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Tweet Search with Python - PythonForBeginners.com<\/title>\n<meta name=\"description\" content=\"Tweet Search with Python will help you improve your python skills with easy to follow examples and tutorials. Click here to view code examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pythonforbeginners.com\/python-on-the-web\/tweet-search-with-python\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Tweet Search with Python - PythonForBeginners.com\" \/>\n<meta property=\"og:description\" content=\"Tweet Search with Python will help you improve your python skills with easy to follow examples and tutorials. Click here to view code examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonforbeginners.com\/python-on-the-web\/tweet-search-with-python\" \/>\n<meta property=\"og:site_name\" content=\"PythonForBeginners.com\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/pythonbeginners\" \/>\n<meta property=\"article:published_time\" content=\"2013-04-22T05:05:14+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-08-28T20:52:21+00:00\" \/>\n<meta name=\"author\" content=\"PFB Staff Writer\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@pythonbeginners\" \/>\n<meta name=\"twitter:site\" content=\"@pythonbeginners\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"PFB Staff Writer\" \/>\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\":\"Article\",\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/python-on-the-web\\\/tweet-search-with-python#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/python-on-the-web\\\/tweet-search-with-python\"},\"author\":{\"name\":\"PFB Staff Writer\",\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/#\\\/schema\\\/person\\\/1f49841b9ba76108d92e7ef1ca5ee648\"},\"headline\":\"Tweet Search with Python\",\"datePublished\":\"2013-04-22T05:05:14+00:00\",\"dateModified\":\"2020-08-28T20:52:21+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/python-on-the-web\\\/tweet-search-with-python\"},\"wordCount\":250,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/#organization\"},\"articleSection\":[\"Code Snippets\",\"Command Line\",\"Python On The Web\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.pythonforbeginners.com\\\/python-on-the-web\\\/tweet-search-with-python#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/python-on-the-web\\\/tweet-search-with-python\",\"url\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/python-on-the-web\\\/tweet-search-with-python\",\"name\":\"Tweet Search with Python - PythonForBeginners.com\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/#website\"},\"datePublished\":\"2013-04-22T05:05:14+00:00\",\"dateModified\":\"2020-08-28T20:52:21+00:00\",\"description\":\"Tweet Search with Python will help you improve your python skills with easy to follow examples and tutorials. Click here to view code examples.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/python-on-the-web\\\/tweet-search-with-python#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pythonforbeginners.com\\\/python-on-the-web\\\/tweet-search-with-python\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/python-on-the-web\\\/tweet-search-with-python#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Tweet Search with Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/#website\",\"url\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/\",\"name\":\"PythonForBeginners.com\",\"description\":\"Learn By Example\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/#organization\",\"name\":\"PythonForBeginners.com\",\"url\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/wp-content\\\/uploads\\\/2020\\\/05\\\/PFB-Logo-Final.png\",\"contentUrl\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/wp-content\\\/uploads\\\/2020\\\/05\\\/PFB-Logo-Final.png\",\"width\":1868,\"height\":318,\"caption\":\"PythonForBeginners.com\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/pythonbeginners\",\"https:\\\/\\\/x.com\\\/pythonbeginners\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/#\\\/schema\\\/person\\\/1f49841b9ba76108d92e7ef1ca5ee648\",\"name\":\"PFB Staff Writer\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/43a6730888902641f2b8e13d1eb78522f178077d85b17a796302635c434a3ac0?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/43a6730888902641f2b8e13d1eb78522f178077d85b17a796302635c434a3ac0?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/43a6730888902641f2b8e13d1eb78522f178077d85b17a796302635c434a3ac0?s=96&d=mm&r=g\",\"caption\":\"PFB Staff Writer\"},\"url\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/author\\\/pfb_staff\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Tweet Search with Python - PythonForBeginners.com","description":"Tweet Search with Python will help you improve your python skills with easy to follow examples and tutorials. Click here to view code examples.","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":"https:\/\/www.pythonforbeginners.com\/python-on-the-web\/tweet-search-with-python","og_locale":"en_US","og_type":"article","og_title":"Tweet Search with Python - PythonForBeginners.com","og_description":"Tweet Search with Python will help you improve your python skills with easy to follow examples and tutorials. Click here to view code examples.","og_url":"https:\/\/www.pythonforbeginners.com\/python-on-the-web\/tweet-search-with-python","og_site_name":"PythonForBeginners.com","article_publisher":"https:\/\/www.facebook.com\/pythonbeginners","article_published_time":"2013-04-22T05:05:14+00:00","article_modified_time":"2020-08-28T20:52:21+00:00","author":"PFB Staff Writer","twitter_card":"summary_large_image","twitter_creator":"@pythonbeginners","twitter_site":"@pythonbeginners","twitter_misc":{"Written by":"PFB Staff Writer","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonforbeginners.com\/python-on-the-web\/tweet-search-with-python#article","isPartOf":{"@id":"https:\/\/www.pythonforbeginners.com\/python-on-the-web\/tweet-search-with-python"},"author":{"name":"PFB Staff Writer","@id":"https:\/\/www.pythonforbeginners.com\/#\/schema\/person\/1f49841b9ba76108d92e7ef1ca5ee648"},"headline":"Tweet Search with Python","datePublished":"2013-04-22T05:05:14+00:00","dateModified":"2020-08-28T20:52:21+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonforbeginners.com\/python-on-the-web\/tweet-search-with-python"},"wordCount":250,"commentCount":0,"publisher":{"@id":"https:\/\/www.pythonforbeginners.com\/#organization"},"articleSection":["Code Snippets","Command Line","Python On The Web"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonforbeginners.com\/python-on-the-web\/tweet-search-with-python#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pythonforbeginners.com\/python-on-the-web\/tweet-search-with-python","url":"https:\/\/www.pythonforbeginners.com\/python-on-the-web\/tweet-search-with-python","name":"Tweet Search with Python - PythonForBeginners.com","isPartOf":{"@id":"https:\/\/www.pythonforbeginners.com\/#website"},"datePublished":"2013-04-22T05:05:14+00:00","dateModified":"2020-08-28T20:52:21+00:00","description":"Tweet Search with Python will help you improve your python skills with easy to follow examples and tutorials. Click here to view code examples.","breadcrumb":{"@id":"https:\/\/www.pythonforbeginners.com\/python-on-the-web\/tweet-search-with-python#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonforbeginners.com\/python-on-the-web\/tweet-search-with-python"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonforbeginners.com\/python-on-the-web\/tweet-search-with-python#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonforbeginners.com\/"},{"@type":"ListItem","position":2,"name":"Tweet Search with Python"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonforbeginners.com\/#website","url":"https:\/\/www.pythonforbeginners.com\/","name":"PythonForBeginners.com","description":"Learn By Example","publisher":{"@id":"https:\/\/www.pythonforbeginners.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pythonforbeginners.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.pythonforbeginners.com\/#organization","name":"PythonForBeginners.com","url":"https:\/\/www.pythonforbeginners.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonforbeginners.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.pythonforbeginners.com\/wp-content\/uploads\/2020\/05\/PFB-Logo-Final.png","contentUrl":"https:\/\/www.pythonforbeginners.com\/wp-content\/uploads\/2020\/05\/PFB-Logo-Final.png","width":1868,"height":318,"caption":"PythonForBeginners.com"},"image":{"@id":"https:\/\/www.pythonforbeginners.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/pythonbeginners","https:\/\/x.com\/pythonbeginners"]},{"@type":"Person","@id":"https:\/\/www.pythonforbeginners.com\/#\/schema\/person\/1f49841b9ba76108d92e7ef1ca5ee648","name":"PFB Staff Writer","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/43a6730888902641f2b8e13d1eb78522f178077d85b17a796302635c434a3ac0?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/43a6730888902641f2b8e13d1eb78522f178077d85b17a796302635c434a3ac0?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/43a6730888902641f2b8e13d1eb78522f178077d85b17a796302635c434a3ac0?s=96&d=mm&r=g","caption":"PFB Staff Writer"},"url":"https:\/\/www.pythonforbeginners.com\/author\/pfb_staff"}]}},"jetpack_publicize_connections":[],"featured_image_src":null,"featured_image_src_square":null,"author_info":{"display_name":"PFB Staff Writer","author_link":"https:\/\/www.pythonforbeginners.com\/author\/pfb_staff"},"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/posts\/5033","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/comments?post=5033"}],"version-history":[{"count":4,"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/posts\/5033\/revisions"}],"predecessor-version":[{"id":8090,"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/posts\/5033\/revisions\/8090"}],"wp:attachment":[{"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/media?parent=5033"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/categories?post=5033"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/tags?post=5033"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}