使用 python 列表中的字典中的 retun statament 从 Twitter 获取推文
Fetching tweets from twitter using retun statament from a dict in list in python
所以下面的函数适用于 print 语句,但不适用于 return stament 如何使用 return 语句
获取推文的计数
>>>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
我在使用上述函数时得到的输出是
>>>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"')
(((((以上输出是针对 'return data'(我只有一条推文,但我需要四条))))))
那么我如何才能 return 推文数量 请帮助我。
您最后 return 只输入了 1 个数据。您需要 return 一个 list
或整个 dict
正如上面评论中指出的那样。
替换:-
for tweet in dict1:
data = tweet['id_str'],tweet['text']
print data
return data
作者:-
data_list = []
for tweet in dict1:
data = tweet['id_str'] + tweet['text']
data_list.append(data)
print data
return data_list
所以下面的函数适用于 print 语句,但不适用于 return stament 如何使用 return 语句
获取推文的计数>>>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
我在使用上述函数时得到的输出是
>>>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"')
(((((以上输出是针对 'return data'(我只有一条推文,但我需要四条))))))
那么我如何才能 return 推文数量 请帮助我。
您最后 return 只输入了 1 个数据。您需要 return 一个 list
或整个 dict
正如上面评论中指出的那样。
替换:-
for tweet in dict1:
data = tweet['id_str'],tweet['text']
print data
return data
作者:-
data_list = []
for tweet in dict1:
data = tweet['id_str'] + tweet['text']
data_list.append(data)
print data
return data_list