如果语句修复列表索引超出范围错误

时间:2019-06-01 20:25:04

标签: python django list if-statement django-views

我正在设置一个调用API的Django视图,并将数据保存到我的数据库中,但是每次循环遇到响应中的最后一项时,我都会不断收到listindex超出范围错误。

这是回溯的样子:

Internal Server Error: /form/
Traceback (most recent call last):
  File "C:\Users\locq\desktop\coding\uvergo_search\venv\lib\site-packages\django\core\handlers\exception.py", line 39, in inner
    response = get_response(request)
  File "C:\Users\locq\desktop\coding\uvergo_search\venv\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\locq\desktop\coding\uvergo_search\venv\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\locq\Desktop\Coding\UVERGO_SEARCH\venv\src\search\views.py", line 126, in api_data
    descriptiontxt = resp_2_data['contentListHtml'][0]['html'][0:2040] + ' ...'
IndexError: list index out of range

如上所述,错误指向第126行:

descriptiontxt = resp_2_data['contentListHtml'][0]['html'][0:2040] + ' ...'

因此,我尝试通过在以下Django视图的行中添加If语句来解决此问题:

def api_data(request):
    if request.GET.get('mybtn'):  # to improve, == 'something':
        resp_1 = requests.get(
            "https://www.headout.com/api/public/v1/product/listing/list-by/city?language=fr&cityCode=PARIS&limit=5000&currencyCode=CAD",
            headers={
                "Headout-Auth": HEADOUT_PRODUCTION_API_KEY
            })
        resp_1_data = resp_1.json()
        base_url_2 = "https://www.headout.com/api/public/v1/product/get/"

        translator = Translator()

        for item in resp_1_data['items']:
            print('parsing, translating and saving item {}'.format(item['id']))
            # concat ID to the URL string
            url = '{}{}'.format(base_url_2, item['id'] + '?language=fr')

            # make the HTTP request
            resp_2 = requests.get(
                url,
                headers={
                    "Headout-Auth": HEADOUT_PRODUCTION_API_KEY
                })
            resp_2_data = resp_2.json()

            descriptiontxt = resp_2_data['contentListHtml'][0]['html'][0:2040] + ' ...'

            #Parse
            soup = BeautifulSoup(descriptiontxt, 'lxml')
            parsed = soup.find('p').text

            #Translate
            translation = translator.translate(parsed, dest='fr')

            titlename = item['name']
            titlefr = translator.translate(titlename, dest='fr')

            destinationname = item['city']['name']
            destinationfr = translator.translate(destinationname, dest='fr')

            Product.objects.get_or_create(
                title=titlefr.text,
                destination=destinationfr.text,
                description=translation.text,
                link=item['canonicalUrl'],
                image=item['image']['url']
            )

            time.sleep(2)

    return render(request, "form.html")

但是我不确定什么是最好的方法。

请帮助!

1 个答案:

答案 0 :(得分:0)

这将停止IndexError的所有情况,但是您可能无法获得想要的行为:

try:
    descriptiontxt = resp_2_data['contentListHtml'][0]['html'][0:2040] + ' ...'
except IndexError:
    descriptiontxt = ' ...'

print(descriptiontxt)

运行此命令时,事先设置了resp_2_data = {'contentListHtml':[]},我得到:

...