0

这是我的代码......它适用于获取个人推文,而不是转推。有人有什么主意吗?:(

import tweepy

CONSUMER_KEY = ''
CONSUMER_SECRET = ''
ACCESS_TOKEN = ''
ACCESS_TOKEN_SECRET = ''


def OAuth():
    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

class CustomStreamListener(tweepy.StreamListener):
    def on_status(self, status):
        try:
                geo = status.geo.get("coordinates")
                lat = geo[0]
                lon = geo[1]
                text = status.text
                print status.id ,status.author.screen_name, text, status.created_at, status.retweeted, status.retweet_count
        except Exception, e:
            #sys.stdout.write("-")
                print "StreamListener Error: %s" %e

    def on_error(self, status_code):
        print("status_code %d")%(status_code)
        return False #Kill the Stream

    def on_timeout(self):
        print("On_timeout")
        return False #Kill the Stream

def main():
    # Call method to establish the connection to twitter
    status_wrapper = TextWrapper(width=60, initial_indent='    ', subsequent_indent='    ')
    auth = OAuth()
    api = tweepy.API(auth)
    api.verify_credentials()
    dateTime = time.strftime("%Y/%m/%d %H:%M:%S")
    text = "Tweet this msg from my app at %s" %dateTime
    api.update_status(text)
    profile = api.get_user('xxxxxxxx')
    user = profile.screen_name
    id = profile.id
    print("%s : %s") %(user, id)
    while True:
            try:
                streaming = tweepy.streaming.Stream(auth, CustomStreamListener(), timeout=3000000000)
                streaming.filter(follow=[id])
            except Exception, e :
                print e

main()

然后,我测试了转发一个人的消息并得到了这个错误......

StreamListener Error: 'NoneType' object has no attribute 'get'

在正常推文的一部分中,它完成了......

367259704327544832 xxxxxxxx Test sending a msg 2013-08-13 12:21:51  False 0
4

1 回答 1

0

我已经找到了这个错误的原因,这要感谢 Taylor Singletary。这是因为我试图从不支持地理标签的转发消息中获取坐标。如果我从代码中删除这 3 行,它会运行良好

geo = status.geo.get("coordinates")
lat = geo[0]
lon = geo[1]

顺便说一句,我仍然对 retweet_count 作为另一张票始终为 0 感到好奇。

于 2013-08-14T04:02:51.690 回答