Twitter 时间轴迭代以使用 for 循环获取我的所有推文

Twitter timeline iteration to get all of my tweets with a for loop

使用此脚本,我正在尝试使用 Twitter api 遍历我的时间线。 我在 "for" 循环中使用时间延迟来将我的所有推文放入这样的对象列表中:

x = [<twitter.status.Status at 0x7fb23b7c5090>, <twitter.status.Status at 0x7fb5fdb7c5090>,<twitter.status.Status at 0x7fbs4b7c5090>,<twitter.status.Status at 0x7f255b7c5090>, ...]

#Setting the connection:
api = twitter.Api(consumer_key= cons_key,
                  consumer_secret = cons_secret,
                  access_token_key = accs_token,
                  access_token_secret = accs_token_secret)

#Getting the latest id and put it on a list called "lis"

x = api.GetUserTimeline(screen_name = "My_Username", count = 1, include_rts = True)
lis=[x[0].id]

#Creating a list to append my twitter status objects.
tweet_info = []

## Iterate through all tweets, say for example 4 times
for i in range(0, 4): 
## tweet extract method with the last list item as the max_id

    user_timeline = api.GetUserTimeline(screen_name="My_Username",
    count=3, include_rts = True, max_id=lis[-1])

    time.sleep(2) ## 2 seconds rest between api calls

    for tweet in range(len(user_timeline)):
    x = user_timeline[tweet]
        tweet_info.append(x)
        lis.append(tweet['id']) #appending the id's in order to my list in order to extract the last one for the next time it iterates.

当我 运行 我的代码出现此错误时:

TypeError                                 Traceback (most recent call last)
<ipython-input-103-11f235fc3b3a> in <module>()
      7         x = user_timeline[tweet]
      8         tweet_info.append(x)
----> 9         lis.append(tweet['id'])

TypeError: 'int' object has no attribute '__getitem__'

我做错了什么,如何解决?

因为tweet是integer

您的代码:

for tweet in range(len(user_timeline)):
    x = user_timeline[tweet]
    tweet_info.append(x)
    lis.append(tweet['id'])

你在内部这样做:

1["id"]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object has no attribute '__getitem__'

即:

 for i in range(len(a)):
    print i
    print i["id"]

 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object has no attribute '__getitem__'