我正在使用python脚本来获取特定用户的关注者。该脚本运行完美,当我使用用户查找API时,它返回关注者的ID,它只返回3个结果。脚本是这样的:
#!/usr/bin/python
from twitter import *
import sys
import csv
import json
config = {}
execfile("/home/oracle/Desktop/twitter-1.17.1/config.py", config)
twitter = Twitter(
auth = OAuth(config["access_key"], config["access_secret"],config["consumer_key"], config["consumer_secret"]))
username = "#####"
query = twitter.followers.ids(screen_name = username)
print "found %d followers" % (len(query["ids"]))
for n in range(0, len(query["ids"]), 100):
ids = query["ids"][n:n+100]
subquery = twitter.users.lookup(user_id = ids)
for user in subquery:
print " [%s] %s" % ("*" if user["verified"] else " ", user["screen_name"])
# print json.dumps(user)
它返回如下输出:
{u'next_cursor_str': u'0', u'previous_cursor': 0, u'ids': [2938672765, 1913345678, 132150958, 2469504797, 2162312397, 737550671029764097, 743699723786158082, 743503916885737473, 742612685632770048, 742487358826811392, 742384945121878020, 741959985127665664, 1541162424, 739102973830254592, 740198523724038144, 542050890, 739971273934176256, 2887662768, 738922874011013120, 738354749045669888, 737638395711791104, 737191937061584896, 329618583, 3331556957, 729645523515396096, 2220176421, 162387597, 727099914635874304, 726665274737475584, 725406360406470657, 938760691, 715260034335305729, 723912842320158720, 538208881, 2188791158, 723558257541828608, 1263571466, 720182865275842564, 719947801598259200, 636067084, 719412219168038912, 719199478260043776, 715921761158574080........ ], u'next_cursor': 0, u'previous_cursor_str': u'0'}
当我使用用户查找API时,它只返回4个屏幕名称,如下所示:
发现了1106个粉丝 [] In_tRu_dEr [] amanhaider3 [] SaaddObaid [] Soerwer
我想要所有ID的屏幕名称,但它只返回4.任何人都可以帮忙。
答案 0 :(得分:1)
你的问题在那两行
(我假设第二行是预定的,虽然它不在问题中)
for n in range(0, len(query["ids"]), 100):
ids = query["ids"][n:n+100]
这些行将创建多个ID数组,并且它们会相互覆盖
所以第一次迭代id将具有从0到100的id
然后用100到200的ID覆盖它,依此类推
直到你从1100到1106的最后一次迭代
所以ids只会有ids
并且从那些6中只有4个被twitter.users.lookup
返回要修复它,你需要将所有内容保存在for n循环中 像这样
for n in range(0, len(query["ids"]), 100):
ids = query["ids"][n:n+100]
subquery = twitter.users.lookup(user_id = ids)
for user in subquery:
print " [%s] %s" % ("*" if user["verified"] else " ", user["screen_name"])
这将起作用