我有一个在Django应用程序上本地运行的函数,但部署后它会引发超出范围的列表索引

2024-06-28 21:00:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个函数,当我试图远程使用它时,它会在日志中引发错误。我让它在我的后台任务中运行。我的另一个任务在本地和远程运行。这是我的密码

我的我的垃圾

def liveleak_task():
    url = 'http://www.example1.com/rss?featured=1'
    name = 'live leak'
    live_leaks = [i for i in feedparser.parse(url).entries]
    the_count = len(live_leaks)
    live_entries = [{'href': live_leak.links[0]['href'],
                     'src': live_leak.media_thumbnail[0]['url'],
                     'text': live_leak.title,
                     'comments': live_leak.description,
                     'name': name,
                     'url': live_leak.links[0]['href'], # this is the link to the source
                     'embed': live_leak.links[1]['href'],
                     'author': None,
                     'video': False
                     } for live_leak in live_leaks][10]
    return live_entries


def worldstar_task():
    # scraping from worldstar
    url_to = 'http://www.example2.com'
    html = requests.get(url_to, headers=headers)
    soup = BeautifulSoup(html.text, 'html5lib')
    titles = soup.find_all('section', 'box')

    def make_soup(url):
        the_comments_page = requests.get(url, headers=headers)
        soupdata = BeautifulSoup(the_comments_page.text, 'html5lib')
        comment = soupdata.find('div')
        para = comment.find_all('p')
        kids = [child.text for child in para]
        blu = str(kids).strip('[]')
        return blu

    name = 'World Star'
    cleaned_titles = [title for title in titles if title.a.get('href') != 'vsubmit.php']
    world_entries = [{'href': url_to + box.a.get('href'),
                      'src': box.img.get('src'),
                      'text': box.strong.a.text,
                      'comments': make_soup(url_to + box.a.get('href')),
                      'name': name,
                      'url': url_to + box.a.get('href'),
                      'embed': None,
                      'author': None,
                      'video': True
                      } for box in cleaned_titles][:10]
    return world_entries


def scrambled(a, b):
    site_one = a
    site_two = b
    merged = site_one+site_two
    random.shuffle(merged)

    for entry in merged:
        post = Post()  #
        post.title = entry['text'] #
        title = post.title  #
        if not Post.objects.filter(title=title):
            post.title = entry['text']
            post.name = entry['name']
            post.url = entry['url']
            post.body = entry['comments']
            post.image_url = entry['src']
            post.video_path = entry['embed']
            post.author = None
            post.video = True
            post.status = 'draft'
            post.save()
            post.tags.add("video")
    return merged

我的任务.py

@app.task
def behind():
    a = worldstar_task()
    b = liveleak_task()
    the_seen = scrambled(a, b)
    return the_seen

我的观点

def noindex(request):
    behind.delay()
    return redirect('/')

追溯

Task blog.tasks.behind[833ef3d3-91e7-4851-99ee-3f7cac4294d6] raised unexpected: IndexError('list index out of range',)
 Traceback (most recent call last):
   File "/app/.heroku/python/lib/python3.5/site-packages/celery/app/trace.py", line 240, in trace_task
     R = retval = fun(*args, **kwargs)
   File "/app/.heroku/python/lib/python3.5/site-packages/celery/app/trace.py", line 438, in __protected_call__
     return self.run(*args, **kwargs)
   File "/app/blog/tasks.py", line 91, in behind
     b = liveleak_task()
   File "/app/blog/my_scraps.py", line 397, in liveleak_task
     } for live_leak in live_leaks][10]
   File "/app/blog/my_scraps.py", line 397, in <listcomp>
     } for live_leak in live_leaks][10]
 IndexError: list index out of range

我试过这个

the_count = len(live_leaks)

然后

for live_leak in live_leaks][the_count -1]

然后

-2它在本地工作,但不是远程工作

然后我试着

:10 didn't work remotely or locally 

没用。不知道为什么,如果有人知道我该怎么解决这个问题,我洗耳恭听


Tags: thetextnameinliveappurlfor
1条回答
网友
1楼 · 发布于 2024-06-28 21:00:30

我已经解决了这个问题。我把[10]从原来的位置移走了

    live_leaks = [i for i in feedparser.parse(url).entries]
    the_count = len(live_leaks)
    live_entries = [{'href': live_leak.links[0]['href'],
                 'src': live_leak.media_thumbnail[0]['url'],
                 'text': live_leak.title,
                 'comments': live_leak.description,
                 'name': name,
                 'url': live_leak.links[0]['href'], # this is the link to the source
                 'embed': live_leak.links[1]['href'],
                 'author': None,
                 'video': False
                 } for live_leak in live_leaks][10]<  here
return live_entries

    live_leaks = [i for i in feedparser.parse(url).entries][:10]<   -to here
    the_count = len(live_leaks)

    live_entries = [{'href': live_leak.links[0]['href'],
                 'src': live_leak.media_thumbnail[0]['url'],
                 'text': live_leak.title,
                 'comments': live_leak.description,
                 'name': name,
                 'url': live_leak.links[0]['href'], # this is the link to the source
                 'embed': live_leak.links[1]['href'],
                 'author': None,
                 'video': False
                 } for live_leak in live_leaks]
return live_entries

我还在10前面加了一个冒号,就像这样

 [:10]  this selects 10

 [10]   this selects the 10th

我希望这对某人有帮助

相关问题 更多 >