Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Twitter流格式JSON输出_Python_Json_Python 3.x_Twitter - Fatal编程技术网

Python Twitter流格式JSON输出

Python Twitter流格式JSON输出,python,json,python-3.x,twitter,Python,Json,Python 3.x,Twitter,也许你能帮我。下面的python代码检索推特流数据,并在获得1000条推特数据时停止。它可以工作,但返回由tab分隔的字段“created\u at、screen\u name和text”。相反,我希望以JSON格式获取数据。如何设置代码以获得JSON格式的数据 # Import the necessary package to process data in JSON format try: import json except ImportError: import simp

也许你能帮我。下面的python代码检索推特流数据,并在获得1000条推特数据时停止。它可以工作,但返回由tab分隔的字段“created\u at、screen\u name和text”。相反,我希望以JSON格式获取数据。如何设置代码以获得JSON格式的数据

# Import the necessary package to process data in JSON format
try:
    import json
except ImportError:
    import simplejson as json

# Import the necessary methods from "twitter" library
from twitter import Twitter, OAuth, TwitterHTTPError, TwitterStream

# Variables that contains the user credentials to access Twitter API


CONSUMER_KEY = '7pWHWtYlXM9ayJfUKv2F8v84B'
CONSUMER_SECRET = 'Dfcx10Px77Ggn0qGbCHc4TZC7M2IHsXpqk9CaGiCLzcr9VMX5n'
ACCESS_TOKEN = '245080367-zuLrIbxblOnocashgku9dsmDKgy3R7uU0VCTIRDx'
ACCESS_SECRET = 'wCx5ufD9Zft46hVjieLdv0af7p9DxUTsPgge9Zm2qelR9'

oauth = OAuth(ACCESS_TOKEN, ACCESS_SECRET, CONSUMER_KEY, CONSUMER_SECRET)

# Initiate the connection to Twitter Streaming API
twitter_stream = TwitterStream(auth=oauth)

# Get a sample of the public data following through Twitter
#iterator = twitter_stream.statuses.sample()

iterator = twitter_stream.statuses.filter(track="Euro2016", language="fr") 

tweet_count = 1000 
for tweet in iterator:
    tweet_count -= 1


    print (tweet['created_at'],"\t",tweet['user']['screen_name'],"\t",tweet['geo'], "\t",tweet['text'])


    if tweet_count <= 0:
        break
#导入必要的包以JSON格式处理数据
尝试:
导入json
除恐怖外:
将simplejson导入为json
#从“twitter”库导入必要的方法
从twitter导入twitter、OAuth、TwitterHTTPError、TwitterStream
#包含访问Twitter API的用户凭据的变量
消费者密钥='7pWHWtYlXM9ayJfUKv2F8v84B'
消费者机密='DFCX10PX77GGN0QGBCHC4TZC7M2IHSQK9CAGICLZCR9VMX5N'
访问令牌='245080367-ZULRIBxBLONOCAHGKU9DSMDKGY3R7UU0VCTIRDX'
访问秘密='wCx5ufD9Zft46hVjieLdv0af7p9DxUTsPgge9Zm2qelR9'
oauth=oauth(访问令牌、访问密钥、使用者密钥、使用者密钥)
#启动与Twitter流媒体API的连接
twitter\u stream=TwitterStream(auth=oauth)
#通过Twitter获取以下公共数据的样本
#迭代器=twitter\u stream.statuses.sample()
迭代器=twitter\u stream.statuses.filter(track=“Euro2016”,language=“fr”)
tweet_计数=1000
对于迭代器中的tweet:
tweet_计数-=1
打印(tweet['created_at']、“\t”、tweet['user']['screen_name']、“\t”、tweet['geo']、“\t”、tweet['text'])

如果tweet_count您可以导入
tweepy
(您需要首先使用pip安装它),并覆盖
侦听器类,以便能够以json格式输出数据。以下是一个例子:

from tweepy import Stream
from tweepy.streaming import StreamListener

#Listener Class Override
class listener(StreamListener):
    def on_data(self, data):
        try: 
            tweet = json.loads(data)
            with open('your_data.json', 'a') as my_file:
                json.dump(tweet, my_file)
        except BaseException:
            print('Error')
            pass

    def on_error(self, status):
        print(statuses)

my_listener=listener()
twitterStream = Stream(oauth, my_listener)  #Inizialize Stream object

您可以在此处阅读有关tweepy的更多信息:

“它可以工作,但会返回字段”created\u at、screen\u name和text“separated by tab”-和?这正是您所要求的。@jonrsharpe我希望输出数据采用JSON格式,因此请更改代码以实现这一点,但当它完全按照所写的格式执行时,请不要感到惊讶。你有问题吗?也许读。