IronPython: Searching for Tweets
[Update on 2009-03-19]
- Simplified example code
- Added link to code sample on my skydrive
This simple example shows you how simple it is to search for tweets using Twitter’s Search API
Key techniques demonstrated in this sample code
- Using System.Net.WebClient to download a file
- Searching for nodes in System.Xml using namespaces
- Simple ATOM-feed parsing
Code is available here: http://cid-19ec39cb500669d8.skydrive.live.com/browse.aspx/Public/IronPython/Samples
#
# Date: 2009-03-19
# Author: Saveen Reddy
# Author email: saveenr@microsoft.com
#
import clr
import System
clr.AddReferenceByPartialName('System.Xml')
import System.Xml
import pprint
def main () :
term='ironpython'
rpp = 100
page = 1
query = r"http://search.twitter.com/search.atom?q=%s&rpp=%s&page=%s" % (term,rpp,page)
content = System.Net.WebClient().DownloadString(query)
dom= System.Xml.XmlDocument()
nsmgr = System.Xml.XmlNamespaceManager( dom.NameTable)
nsmgr.AddNamespace( "atom", "http://www.w3.org/2005/Atom") ;
dom.LoadXml( content )
root = dom.DocumentElement
entry_nodes = [ n for n in root.SelectNodes( "atom:entry" ,nsmgr) ]
def get_prop( n , q) :
return n.SelectSingleNode(q,nsmgr).InnerText
text = [ ( get_prop(n,"atom:updated"),
get_prop(n,"atom:author/atom:name") ,
get_prop(n,"atom:title") )
for n in entry_nodes]
pprint.pprint(text)
if ( __name__ == '__main__' ) :
main()