D
D
DenDuatlov2021-09-18 02:43:07
Python
DenDuatlov, 2021-09-18 02:43:07

Parser error in python. IndexError: list index out of range. how to fix?

import requests
from bs4 import BeautifulSoup as BS
import telebot


zodiacs = {"Козерог": {"unicode": 9809, "latin": "Capricon"} }


def get_horoscope(zodiac, URL='https://horoscopes.rambler.ru'):
    ASKURL = f'{URL}/{zodiac.lower()}/'
    HEADERS = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.0; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0'}
    response = requests.get(ASKURL, headers=HEADERS)
    soup = BS(response.content, 'html.parser')
    item = soup.findAll('div', class_='_1E4Zo _3BLIa')[0]
    parse_result = item.find('p', class_='mtZOt').get_text(strip=True)
    return parse_result


def get_info(zodiac):
    ask_horo = zodiacs[zodiac].get('latin')
    uni_horo = chr(zodiacs[zodiac].get('unicode'))
    result_horo = get_horoscope(ask_horo)
    return f'Результаты для знака зодиака {zodiac}- {ask_horo} - {uni_horo}\n{result_horo}'


my_horo = get_info('Козерог')

print(my_horo)


Gives this error

PS C:\Users\DenDuatlov\Desktop\Bot_Horoskpe\new_bot17.09.2021> & C:/Users/DenDuatlov/AppData/Local/Programs/Python/Python38-32/python.exe c:/Users/DenDuatlov/Desktop /Bot_Horoskpe/new_bot17.09.2021/capricon.py
Traceback (most recent call last):
File "c:/Users/DenDuatlov/Desktop/Bot_Horoskpe/new_bot17.09.2021/capricon.py", line 27, in
my_horo = get_info('Capricorn ')
File "c:/Users/DenDuatlov/Desktop/Bot_Horoskpe/new_bot17.09.2021/capricon.py", line 23, in get_info
result_horo = get_horoscope(ask_horo)
File "c:/Users/DenDuatlov/Desktop/Bot_Horoskpe/new_bot17. 09.2021/capricon.py", line 15, in get_horoscope
item = soup.findAll('div', class_='_1E4Zo _3BLIa')[0]
IndexError: list index out of range

Please tell me how to fix it? I found several solutions on the Internet, but they are for mathematical problems. I do not understand how to adapt them to this code.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
longclaps, 2021-09-18
@longclaps

item = soup.findAll('div', class_='_1E4Zo _3BLIa')[0]
You are trying to access an empty list.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question