我正在尝试从 twitter 下载推文。
为此,我使用了 python 和 Tweepy。虽然我对 Python 和 Twitter API 都很陌生。
我的 Python 脚本如下:#!usr/bin/python
#import modules
import sys
import tweepy
import json
#global variables
consumer_key = ''
consumer_secret = ''
token_key = ''
token_secret = ''
#Main function
def main():
print sys.argv[0],'starts'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(token_key, token_secret)
print 'Connected to Twitter'
api = tweepy.API(auth)
if not api.test():
print 'Twitter API test failed'
print 'Experiment with cursor'
print 'Get search method returns json objects'
json_search = api.search(q="football")
#json.loads(json_search())
print json_search
#Standard boilerplate to call main function if this file runs
if __name__ == '__main__':
main()
我得到的结果如下:
[<tweepy.models.SearchResult object at 0x9a0934c>, <tweepy.models.SearchResult object at 0x9a0986c>, <tweepy.models.SearchResult object at 0x9a096ec>, <tweepy.models.SearchResult object at 0xb76d8ccc>, <tweepy.models.SearchResult object at 0x9a09ccc>, <tweepy.models.SearchResult object at 0x9a0974c>, <tweepy.models.SearchResult object at 0x9a0940c>, <tweepy.models.SearchResult object at 0x99fdfcc>, <tweepy.models.SearchResult object at 0x99fdfec>, <tweepy.models.SearchResult object at 0x9a08cec>, <tweepy.models.SearchResult object at 0x9a08f4c>, <tweepy.models.SearchResult object at 0x9a08eec>, <tweepy.models.SearchResult object at 0x9a08a4c>, <tweepy.models.SearchResult object at 0x9a08c0c>, <tweepy.models.SearchResult object at 0x9a08dcc>]
现在我很困惑如何从这些信息中提取推文?我尝试对这些数据使用 json.loads 方法。但它给了我错误,因为 JSON 需要字符串或缓冲区。示例代码将不胜感激。提前致谢。