【发布时间】:2015-07-30 10:15:46
【问题描述】:
我正在尝试使用姓名列表查询 Twitter API 并获取他们的朋友列表。 API 部分很好,但我不知道如何检查前 5 个名称,提取结果,等待一段时间以遵守速率限制,然后再为接下来的 5 个名称重复,直到列表结束。我遇到问题的代码是这样的:
first = 0
last = 5
while last < 15: #while last group of 5 items is lower than number of items in list#
for item in list[first:last]: #parses each n twitter IDs in the list#
results = item
text_file = open("output.txt", "a") #creates empty txt output / change path to desired output#
text_file.write(str(item) + "," + results + "\n") #adds twitter ID, resulting friends list, and a line skip to the txt output#
text_file.close()
first = first + 5 #updates list navigation to move on to next group of 5#
last = last + 5
time.sleep(5) #suspends activities for x seconds to respect rate limit#
这个脚本不应该遍历列表中的前 5 个项目,将它们添加到输出文件,然后更改 first:last 参数并循环它直到“last”变量为 15 或更高?
【问题讨论】:
标签: python api for-loop twitter while-loop