【发布时间】:2019-01-27 22:30:17
【问题描述】:
我一直在编写一个脚本,以便在日志中删除对单个推文的回复
现在,我自己还没有完成这一切,最后让它几乎可以工作,但我几乎在最后发现了一个索引错误,“列表索引超出范围”
我有点困惑,因为我看不出这里有什么问题......有人可以解释一下吗? ._.
def tweet_url(t):
return "https://twitter.com/%s/status/%s" % (t.user.screen_name, t.id)
def get_tweets(filename):
for line in open(filename):
yield twitter.Status.NewFromJsonDict(json.loads(line))
def get_replies(tweet):
user = tweet.user.screen_name
tweet_id = tweet.id
max_id = None
logging.info("looking for replies to: %s" % tweet_url(tweet))
while True:
q = urllib.parse.urlencode({"q": "to:%s" % user})
try:
replies = t.GetSearch(raw_query=q, since_id=tweet_id, max_id=max_id, count=100)
except twitter.error.TwitterError as e:
logging.error("caught twitter api error: %s", e)
time.sleep(60)
continue
for reply in replies:
logging.info("examining: %s" % tweet_url(reply))
if reply.in_reply_to_status_id == tweet_id:
logging.info("found reply: %s" % tweet_url(reply))
yield reply
# recursive magic to also get the replies to this reply
for reply_to_reply in get_replies(reply):
yield reply_to_reply
max_id = reply.id
if len(replies) != 100:
break
if __name__ == "__main__":
logging.basicConfig(filename="replies.log", level=logging.INFO)
tweets_file = sys.argv[1]
for tweet in get_tweets(tweets_file):
for reply in get_replies(tweet):
print(reply.AsJsonString())
所以...在底线,列表 (sys.argv [1]) 导致了这里的问题,但我不明白为什么会出现超出范围的索引错误,知道吗?
【问题讨论】:
-
你能发布完整的错误信息吗?
标签: python list twitter tweepy