Reputation: 1171
So the below function is working for print statement but not for return stament How can i get the count no of tweets using return statement
>>>import requests
>>>from requests_oauthlib import OAuth1
>>>import urllib
# Credentials to fetch
>>>consumer_key = '********'
>>>consumer_secret = '******************'
>>>url = 'https://api.twitter.com/1.1/search/tweets.json'
>>>def get_keyword_tweets(keyword, Count):
param = urllib.urlencode({'q': keyword, 'lang': 'en', 'result_type':
'recent', 'count': Count})
url2 = url.endswith('?') and (url+param) or (url + '?' + param)
auth = OAuth1(consumer_key, consumer_secret)
r = requests.get(url2, auth=auth)
tweets = r.json()
keywordtweets = tweets['statuses']
dict1 = keywordtweets[0:Count]
#data = dict1['id_str'],dict1['text']
for tweet in dict1:
data = tweet['id_str'],tweet['text']
print data
return data
The output i am getting when i am using the above function is
>>>In [1]: from http_twitter import get_keyword_tweets
>>>In [2]: get_keyword_tweets("CWC15",Count=4)
(u'578172948231495680', u'RT @ICC: Fascinating stat from Pool Stages with the breakdown of wickets in the tournament, just wait for #AUSvPAK!! \n#cwc15 http://t.co/Jw\u2026')
(u'578172941977808896', u'RT @Surbhivb: Venkat: on the UAE cricket team, led by Khurram Khan, an airline purser. Only fully amateur team in the #CWC15. http://t.co/c\u2026')
(u'578172938467176448', u'RT @iTweety_19: "I am ready to go to the World Cup if the selectors pick me as the replacement for Mohammad Irfan" says @REALsaeedajmal \n#c\u2026')
(u'578172935115960320', u'Thank you @KumarSanga2 & @MahelaJay for all the epic partnerships! #ThankYouSanga #ThankYouMahela #SAvSL #CWC15 http://t.co/li0QgPniI0"')
(((((The above output is for print data"(i got 4 tweets as i mentioned)")))))
Out[2]:
(u'578172935115960320',
u'Thank you @KumarSanga2 & @MahelaJay for all the epic partnerships! #ThankYouSanga #ThankYouMahela #SAvSL #CWC15 http://t.co/li0QgPniI0"')
(((((The above output is for 'return data'(I got only one tweet but i need four))))))
so how can i return the count no of tweets Please help me.
Upvotes: 0
Views: 43
Reputation: 1468
You are returning just 1 data at the end. You need to return a list
or the whole dict
as pointed out in a comment above.
replace:-
for tweet in dict1:
data = tweet['id_str'],tweet['text']
print data
return data
By:-
data_list = []
for tweet in dict1:
data = tweet['id_str'] + tweet['text']
data_list.append(data)
print data
return data_list
Upvotes: 1