Why use BeautifulSoup find_all method will results in an error(list index out of range)?

80 Views Asked by At

The html like this:

<div id="market-summary">
  ...
   <li>
     <span class="title">title1</span>
     <span class="subtitle">subtitle1</span>
   <li>
   <li>
     <span class="title">title2</span>
     <span class="subtitle">subtitle2</span>
   <li>
   <li>
     <span class="title">title1</span>
     <span class="subtitle">subtitle1</span>
   <li>
  ...
</div>

python code almost like this:

........
if soup.select("#market-summary")[0].findAll('li'):
    prices = soup.select("#market-summary")[0].findAll('li')
    if prices is not None and len(prices) > 0:
        price = [
            {'size': x.find(class_="title").get_text(),
             'price': x.find(class_="subtitle").get_text()}
            for x in prices
        ]
        return price
    return 'price?'
return 'li?'
........
........

Now I start running this program the program is running. and The console was printing the price but just two or three mins, I got error message:

'IndexError: list index out of range'

and program interrupted and i tried it again and again. the always 'IndexError: list index out of range' why the didn't show me 'price?' or 'li?'. Case BeautifulSoup?

1

There are 1 best solutions below

1
On BEST ANSWER

It means that the page you are parsing does not have any elements with id market-summary
If you check the error produced again, you might find that the error is occurring in line 1 i.e. if soup.select("#market-summary")[0] or line 2 i.e. prices = soup.select("#market-summary")[0].findAll('li')