Python 2.7 获取与tweet对应的各种数据
我正在尝试从twitter获取数据进行处理。请看代码我想要各种数据对应一个特定的tweet对应一个给定的主题。我能够获取数据(创建时间、文本、用户名、用户id)。当我尝试获取(位置、追随者计数、朋友计数、转发计数)时,它显示错误Python 2.7 获取与tweet对应的各种数据,python-2.7,twitter,tweepy,Python 2.7,Twitter,Tweepy,我正在尝试从twitter获取数据进行处理。请看代码我想要各种数据对应一个特定的tweet对应一个给定的主题。我能够获取数据(创建时间、文本、用户名、用户id)。当我尝试获取(位置、追随者计数、朋友计数、转发计数)时,它显示错误 它在all_data[“location”]上失败的原因是tweets没有这样的属性: 与好友数、追随者数相同——它们是用户的属性,而不是推特 代码不应该在所有日期[“retweet\u count”]失败,因为tweet具有这样的属性 注意:报告错误时,请包括错误
它在
all_data[“location”]
上失败的原因是tweets没有这样的属性:
- 与好友数、追随者数相同——它们是用户的属性,而不是推特
注意:报告错误时,请包括错误消息(即使您跳过完整的错误回溯)。使帮助您变得更容易,否则您必须猜测错误可能是什么。谢谢,这帮了大忙。我得到了同样的回复。对于followers\u count,我使用followers\u count=str(所有followers数据[“user”][“followers\u count”]),对于其他3个。。。。。。
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import time
import json
ckey = '***********************'
csecret = '************************'
atoken ='*************************'
asecret = '**********************'
class listener(StreamListener):
def on_data(self,data):
try:
all_data = json.loads(data)
tweet = all_data["text"]
username = all_data["user"]["screen_name"]
timestamp = all_data["created_at"]
user_id = all_data["id_str"]
location = all_data["location"]
followers_count = all_data["followers_count"]
friends_count = all_data["friends_count"]
retweet_count = all_data["retweet_count"]
saveThis = str(time.time())+'::'+timestamp+'::'+username+'::'+user_id+'::'+tweet+'::'+followers_count+'::'+friends_count+'::'+retweet_count+'::'+location
saveFile = open('clean2.txt','a')
saveFile.write(saveThis)
saveFile.write('\n')
saveFile.close
return True
except BaseException, e:
print 'failed on data,',str(e)
time.sleep(5)
def on_error(self, status):
print status
auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
twitterStream = Stream(auth, listener())
twitterStream.filter(track=["tweepy"])#topic