S
S
Switchback092019-10-20 21:56:03
beautiful soup
Switchback09, 2019-10-20 21:56:03

Beautiful soup, list index out of range how to fix?

I looked at the element code and found what I needed for 'namePlayer', it was 4th column and 'a' tag. And I tried to find it in answers.appendvia cols[3].a.text
But when I compile the code I get an IndexError. After I tried to change the index to 2, 3, 4, 5 but nothing changed
Question: why do I get an IndexError: list index out of range error when everything is correct (kind of like :D)
Code:

#!/usr/bin/env python3

import re
import urllib.request
from bs4 import BeautifulSoup

class AppURLopener(urllib.request.FancyURLopener):
    version = "Mozilla/5.0"


def get_html(url):
    opener = AppURLopener()
    response = opener.open(url)
    return response.read()

def parse(html):
    soup = BeautifulSoup(html)
    table = soup.find(id='answers')

    answers = []

    for row in table.find_all('div')[16:]:
        cols = row.find_all('div')

    answers.append({
        'namePlayer': cols[3].a.text
    })


    for answer in answers:
        print(answers)

def main():
    parse(get_html('http://jaze.ru/forum/topic?id=50&page=1'))

if __name__ == '__main__':
    main()

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question