首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >danbooru twitter bot上的索引错误列表页超出范围

danbooru twitter bot上的索引错误列表页超出范围
EN

Stack Overflow用户
提问于 2018-08-30 03:22:56
回答 1查看 217关注 0票数 0

我正在使用我在github中找到的代码,我不得不修改一些东西,它可以工作,但有时(即使在工作时)它会给出索引错误页面超出范围,然后停止工作。

文件"bot.py",第36行,模块中

imageSource = pageTablearrayNum

IndexError:列表索引超出范围

以下是我的代码

代码语言:javascript
复制
import time                                                                                                                                                                                                        
import requests                                                                                                                                                                                                    
import tweepy                                                                                                                                                                                                      
import urllib                                                                                                                                                                                                      
import os                                                                                                                                                                                                          
import random                                                                                                                                                                                                      

page = 1                                                                                                                                                                                                           

url = 'https://danbooru.donmai.us/posts.json?tags=shimakaze_(kantai_collection) rating:s&limit=1000&page='                                                                                                         

consumer_key = ''                                                                                                                                                                         
consumer_secret = ''                                                                                                                                             
access_key = ''                                                                                                                                                  
access_secret = ''                                                                                                                                                    

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)                                                                                                                                                          
auth.set_access_token(access_key, access_secret)                                                                                                                                                                   

api = tweepy.API(auth)                                                                                                                                                                                             

while True:                                                                                                                                                                                                        
        try:                                                                                                                                                                                                       
                random.seed()                                                                                                                                                                                      
                jsURL = url + str(random.randint(1,1000))                                                                                                                                                          
                response = requests.get(jsURL)                                                                                                                                                                     
                pageTable = response.json()                                                                                                                                                                        
                arrayNum = random.randint(0,5)                                                                                                                                                                     

                print arrayNum                                                                                                                                                                                     
                imageSource = pageTable[arrayNum]["file_url"]                                                                                                                                                      
                imageURL = imageSource                                                                                                                                                                             
                print imageURL                                                                                                                                                                                     
                sourceURL = "http://danbooru.donmai.us/posts/" + str(pageTable[arrayNum]["id"])                                                                                                                    
                print sourceURL                                                                                                                                                                                    
                urllib.urlretrieve(imageURL, 'image.jpg')                                                                                                                                                          

                .                                                                                                                                                
                tweetString = sourceURL + " "                                                                                                                                                                      
                api.update_with_media('image.jpg', status=tweetString)                                                                                                                                             

                os.remove('image.jpg')                                                                                                                                                                             
               post. Limited to 500 requests/hour.                                                                                                     
                time.sleep(600)                                                                                                                                                                                    

        except tweepy.error.TweepError:                                                                                                                                                                            
                print "Image too large, finding a different image.." 

arrayNum = random.randint(0,5)代码行给出了错误,该代码生成了一个0-5的数字,并用作danbooru页面,所以我不知道为什么它会给出一个IndexError

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52084719

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档