D
D
Daniil Mishchenko2020-11-18 18:48:44
Python
Daniil Mishchenko, 2020-11-18 18:48:44

I don't understand what's wrong with the parser code?

According to the video tutorial, I wrote a parser for a site with games, but when executed, it gives the error
"IndexError: list index out of range"
If you remove "[0].text" from the code at the end, it gives a list of games, but with html elements, help me do this to give a list of games without html elements

import requests
from bs4 import BeautifulSoup as BS

def parse():
  URL = requests.get( 'https://stopgame.ru/review/new/stopchoise' )
  html = BS( URL.content, 'html.parser' )

  for element in html.select( '.item' ):
    title = element.select( '.caption > a' )
    print( title[0].text )

parse()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2020-11-18
@lacevedoufe

It's enough

for element in html.select( '.item .caption > a' ):
    print( element.text )

It's just that .item is not only blocks with games (but, for example, also menu items that do not have a title.caption).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question