【问题标题】:return actual tweets in tweepy?在 tweepy 中返回实际的推文?
【发布时间】:2011-12-04 14:00:46
【问题描述】:

我正在使用 tweepy 编写一个 twitter 程序。当我运行此代码时,它会为它们打印 Python ... 值,例如

<tweepy.models.Status object at 0x95ff8cc>

这不好。我如何获得实际的推文?

import tweepy, tweepy.api
key = XXXXX
sec = XXXXX

tok  = XXXXX
tsec = XXXXX

auth = tweepy.OAuthHandler(key, sec)
auth.set_access_token(tok, tsec)
api = tweepy.API(auth)

pub = api.home_timeline()
for i in pub:
        print str(i)

【问题讨论】:

  • 谢谢。这对于一般对象非常有用。

标签: python twitter tweepy


【解决方案1】:

一般来说,您可以使用 Python 中的 dir() 内置函数来检查对象。

这里似乎缺少 Tweepy 文档,但我认为 Status 对象反映了 Twitter 的 REST 状态格式的结构,请参阅(例如)https://dev.twitter.com/docs/api/1/get/statuses/home_timeline

所以——试试

print dir(status)

查看状态对象中的内容

或者只是说,

print status.text
print status.user.screen_name

【讨论】:

  • dir() 是一个了不起的功能
【解决方案2】:

看看 getstate() get 方法,它可以用来检查返回的对象

for i in pub:
    print i.__getstate__()

【讨论】:

    【解决方案3】:

    api.home_timeline() 方法返回一个包含 20 个 tweepy.models.Status 对象的列表,这些对象对应于前 20 条推文。也就是说,每条推文都被视为状态类的一个对象。每个 Status 对象都有许多属性,例如 id、text、user、place、created_at 等。

    以下代码将打印推文 ID 和文本:

    tweets = api.home_timeline()
    for tweet in tweets:
      print tweet.id, " : ", tweet.text
    

    【讨论】:

      【解决方案4】:

      来自实际的推文,如果你想要特定的推文,你必须有一个推文ID, 并使用

      tweets = self.api.statuses_lookup(tweetIDs)
      for tweet in tweets:
        #tweet obtained
        print(str(tweet['id'])+str(tweet['text']))
      

      或者如果你想要一般的推文 使用推特流 api

      class StdOutListener(StreamListener):
      def __init__(self, outputDatabaseName, collectionName):
          try:
              print("Connecting to database")
              conn=pymongo.MongoClient()
              outputDB = conn[outputDatabaseName]
              self.collection = outputDB[collectionName]
              self.counter = 0
          except pymongo.errors.ConnectionFailure as e:
              print ("Could not connect to MongoDB:")
      def on_data(self,data): 
          datajson=json.loads(data)
          if "lang" in datajson and datajson["lang"] == "en" and "text" in datajson:
              self.collection.insert(datajson)
      
              text=datajson["text"].encode("utf-8") #The text of the tweet
              self.counter += 1
              print(str(self.counter) + " " +str(text))
      
      def on_error(self, status):
          print("ERROR")
          print(status)
      def on_connect(self):
          print("You're connected to the streaming server.
      l=StdOutListener(dbname,cname)
          auth=OAuthHandler(Auth.consumer_key,Auth.consumer_secret)
          auth.set_access_token(Auth.access_token,Auth.access_token_secret)
          stream=Stream(auth,l)
      
      
          stream.filter(track=stopWords)
      

      创建一个继承自 StreamListener 的类 Stdoutlistener 覆盖on_data函数,并以json格式返回tweet,每次获取tweet时都会运行该函数 根据停用词过滤推文 这是您在推文中想要的单词列表

      【讨论】:

        【解决方案5】:

        在 tweepy Status 实例上,您可以访问_json 属性,该属性返回一个表示原始Tweet contents 的字典。

        例如:

        type(status)
        # tweepy.models.Status
        
        type(status._json)
        # dict
        
        status._json.keys()
        # dict_keys(['favorite_count', 'contributors', 'id', 'user', ...])
        

        【讨论】:

          猜你喜欢
          • 2016-09-20
          • 2018-10-20
          • 1970-01-01
          • 2014-10-24
          • 2020-12-03
          • 2016-11-04
          • 2021-03-28
          • 2015-12-19
          • 1970-01-01
          相关资源
          最近更新 更多