【问题标题】:fetching various data corresponding to a tweet获取与推文对应的各种数据
【发布时间】:2016-02-28 02:04:11
【问题描述】:

我正在尝试从 twitter 获取数据进行处理。请查看代码 我想要与给定主题对应的特定推文对应的各种数据。我能够获取数据(created_at、文本、用户名、user_id)。当我尝试获取时它显示错误(位置,followers_count,friends_count,retweet_count)。

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

【问题讨论】:

    标签: python-2.7 twitter tweepy


    【解决方案1】:

    all_data["location"] 上失败的原因是推文没有这样的属性:https://dev.twitter.com/overview/api/tweets

    • friends_count、followers_count 相同 - 它们是用户的属性,而不是推文。

    代码不应该在all_date["retweet_count"] 上失败,因为推文具有这样的属性。

    附:请在报告错误时包含错误消息(即使您跳过完整的错误引用)。帮助您更容易,否则您必须猜测错误可能是什么。

    【讨论】:

    • 感谢它帮了大忙。我得到了 retweet_count 。对于 followers_count 我使用了 followers_count = str(all_data["user"]["followers_count"]) 和其他 3 类似......
    猜你喜欢
    • 2019-08-21
    • 1970-01-01
    • 2021-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多