Twitter Bot: Difference between revisions
Migratebot (talk | contribs) (Created page with "<source lang="python"> from urllib import urlencode
import urllib2
import json
def openURL (url, user_agent="Mozilla/5.0 (X11; U; Linux x86_64; fr; rv:1.9.1.5) Gecko/20091...") |
No edit summary |
||
Line 1: | Line 1: | ||
Use feedparser! | |||
<source lang="python"> | |||
import feedparser | |||
url = "http://search.twitter.com/search.atom?q=feel" | |||
feed = feedparser.parse(url) | |||
for e in feed.entries: | |||
print e.title.encode("utf-8") | |||
</source> | |||
<source lang="python"> | <source lang="python"> | ||
from urllib import urlencode | from urllib import urlencode |
Revision as of 21:59, 6 December 2010
Use feedparser!
import feedparser
url = "http://search.twitter.com/search.atom?q=feel"
feed = feedparser.parse(url)
for e in feed.entries:
print e.title.encode("utf-8")
from urllib import urlencode
import urllib2
import json
def openURL (url, user_agent="Mozilla/5.0 (X11; U; Linux x86_64; fr; rv:1.9.1.5) Gecko/20091109 Ubuntu/9.10 (karmic) Firefox/3.5.5"):
"""
Returns: tuple with (file, actualurl)
sets user_agent & follows redirection if necessary
realurl maybe different than url in the case of a redirect
"""
request = urllib2.Request(url)
if user_agent:
request.add_header("User-Agent", user_agent)
pagefile=urllib2.urlopen(request)
realurl = pagefile.geturl()
return (pagefile, realurl)
def getJSON (url):
(f, url) = openURL(url)
return json.loads(f.read())
TWITTER_SEARCH = "http://search.twitter.com/search.json"
data = getJSON(TWITTER_SEARCH + "?" + urlencode({'q': 'Rotterdam'}))
for r in data['results']:
print r['text']