Reputation: 10567
I'm a starter in python.I use the following code to get tweets depending on a input query.
import urllib
import urllib2
import json
def getData(keyword):
url = 'http://search.twitter.com/search.json'
data = {'q': keyword, 'lang': 'en', 'result_type': 'recent'}
params = urllib.urlencode(data)
try:
req = urllib2.Request(url, params)
response = urllib2.urlopen(req)
jsonData = json.load(response)
tweets = []
for item in jsonData['results']:
tweets.append(item['text'])
return tweets
except urllib2.URLError, e:
self.handleError(e)
return tweets
tweets = getData("messi")
print tweet
but i get the following error in the above code. Name Error: global name 'self' is not defined. How can i correct this error?
Upvotes: 1
Views: 7946
Reputation: 1492
Like ChrisP said, first you need to remove self.
from your code.
then you might get another error as handleError function is not defined anywhere in your code. So, You have to define the handleError function as well if you have not defined it yet.
Take a look at the python doc to learn more about classes and objects.
import urllib
import urllib2
import json
#defining handleError
def handleError(e):
#Error Handling code goes here
def getData(keyword):
url = 'http://search.twitter.com/search.json'
data = {'q': keyword, 'lang': 'en', 'result_type': 'recent'}
params = urllib.urlencode(data)
try:
req = urllib2.Request(url, params)
response = urllib2.urlopen(req)
jsonData = json.load(response)
tweets = []
for item in jsonData['results']:
tweets.append(item['text'])
return tweets
except urllib2.URLError, e:
handleError(e) #removed self.
return tweets
tweets = getData("messi")
print tweet
Upvotes: 1