【问题标题】:Return a users tweets with tweepy使用 tweepy 返回用户的推文
【发布时间】:2014-10-24 15:29:55
【问题描述】:

我正在使用 tweepy 和 python 2.7.6 返回指定用户的推文

我的代码如下:

import tweepy

ckey = 'myckey'
csecret = 'mycsecret'
atoken = 'myatoken'
asecret = 'myasecret'



auth = tweepy.OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)

api = tweepy.API(auth)

stuff = api.user_timeline(screen_name = 'danieltosh', count = 100, include_rts = True)

print stuff

然而这会产生一组看起来像<tweepy.models.Status object at 0x7ff2ca3c1050>的消息

是否可以从这些对象中打印出有用的信息?我在哪里可以找到他们的所有属性?

【问题讨论】:

标签: python twitter tweepy


【解决方案1】:

不幸的是,Status 模型在tweepy docs 中没有得到很好的记录。

user_timeline() 方法返回Status 对象实例的列表。您可以使用dir() 探索可用的属性和方法,或查看actual implementation。

比如从源码中可以看到有author、user等属性:

for status in stuff:
    print status.author, status.user

或者,您可以打印出包含 API 调用实际响应的 _json 属性值:

for status in stuff:
    print status._json

【讨论】:

  • 所以我会简单地输入print dir(stuff) ?
  • @user8028 for status in stuff: print dir(status).
  • 那么我可以做一个类似的循环来打印text 属性,例如for status in stuff: print status.text 或dir() 列出的任何其他属性吗?
  • @user8028 是的,对每个status 所需的每个属性都使用点符号。
  • 这可能有点偏离主题,但仍然符合属性,但是您可以直接访问用作属性的主题标签还是有必要解析文本属性?
【解决方案2】:
import tweepy
import tkinter

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
# set parser=tweepy.parsers.JSONParser() if you want a nice printed json response.

userID = "userid"
user = api.get_user(userID)

tweets = api.user_timeline(screen_name=userID, 
                           # 200 is the maximum allowed count
                           count=200,
                           include_rts = False,
                           # Necessary to keep full_text 
                           # otherwise only the first 140 words are extracted
                           tweet_mode = 'extended'
                           )

for info in tweets[:3]:
    print("ID: {}".format(info.id))
    print(info.created_at)
    print(info.full_text)
    print("\n")

感谢https://fairyonice.github.io/extract-someones-tweet-using-tweepy.html

【讨论】:

    猜你喜欢
    • 2015-12-19
    • 1970-01-01
    • 2012-03-08
    • 1970-01-01
    • 2016-09-20
    • 2018-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多