Liv*_*ool 1 python twitter json tweepy
我想从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()
Run Code Online (Sandbox Code Playgroud)
我得到的结果如下:
[<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>]
Run Code Online (Sandbox Code Playgroud)
现在我很困惑如何从这些信息中提取推文?我试图对这些数据使用json.loads方法.但它给了我错误,因为JSON需要字符串或缓冲区.示例代码将受到高度赞赏.提前致谢.
Tweepy为您提供更丰富的物品; 它为你解析了JSON.
这些SearchResult对象具有与Twitter发送的JSON结构相同的属性; 只需查看Tweet文档即可查看可用内容:
for result in api.search(q="football"):
print result.text
Run Code Online (Sandbox Code Playgroud)
演示:
>>> import tweepy
>>> tweepy.__version__
'3.3.0'
>>> consumer_key = '<consumer_key>'
>>> consumer_secret = '<consumer_secret>'
>>> access_token = '<access_token>'
>>> access_token_secret = '<access_token_secret>'
>>> auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
>>> auth.set_access_token(access_token, access_token_secret)
>>> api = tweepy.API(auth)
>>> for result in api.search(q="football"):
... print result.text
...
Great moments from the Women's FA Cup http://t.co/Y4C0LFJed9
RT @freebets: 6 YEARS AGO TODAY:
Football lost one of its great managers.
RIP Sir Bobby Robson. http://t.co/NCo90ZIUPY
RT @Oddschanger: COMPETITION CLOSES TODAY!
Win a Premier League or Football League shirt of YOUR choice!
RETWEET & FOLLOW to enter. http…
Berita Transfer: Transfer rumours and paper review – Friday, July 31 http://t.co/qRrDIEP2zh [TS] #nobar #gosip
@ajperry18 im sorry I don't know this football shit
@risu_football ???????????????
NFF Unveils Oliseh As Super Eagles Coach - SUNDAY Oliseh has been unveiled by the Nigeria Football... http://t.co/IOYajD9bi2 #Sports
RT @BilelGhazi: RT @lequipe : Gourcuff, au tour de Guingamp http://t.co/Dkio8v9LZq
@EDS_Amy HP SAUCE ?
RT @fsntweet: ?????????????????????????????????? - http://t.co/yg5iuABy3K
???????????????????????????????????????????????????
RT @peterMwendo: Le football cest un sport collectif ou on doit se faire des passe http://t.co/61hy138yo8
RT @TSBible: 6 years ago today, football lost a true gentleman. Rest in Peace Sir Bobby Robson. http://t.co/6eHTI6UxaC
6 years ago today the greatest football manger of all time passed away SIR Bobby Robson a true Ipswich and footballing legend
The Guardian: PSG close to sealing £40m deal for Manchester United’s Ángel Di María. http://t.co/gAQEucRLZa
Sir Bobby Robson, the #football #legend passed away 6 years ago.
#Barcelona #newcastle #Porto http://t.co/4UXpnvrHhS
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13922 次 |
| 最近记录: |