0

我制作了一个简单的应用程序,它使用 Twitter 搜索 API 并根据主题名称查找推文并将文本存储在文本文件中。我每页收到 100 条推文,但有时我收到“OutOfRange”索引错误,执行就停在那里。

topic = raw_input('Enter the topic name: ')

f = open('out.txt', 'w')

for y in range(1, 15):
    j = urllib2.urlopen('http://search.twitter.com/search.json?q='+topic+'&rpp=100&page='+str(y))
    print('Done with page #'+str(y))
    j_obj = json.load(j)
    for x in range(1, 100):
        current = j_obj['results'][x]['text']
        f.write(str(''.join(c for c in current if ord(c) < 128))+'\n')
f.close()

知道如何解决这个问题并完成程序执行吗?

我收到错误:

current = j_obj['results'][x]['text']
IndexError: list index out of range
4

0 回答 0