为什么使用 BeautifulSoup find_all 方法会导致错误(列表索引超出范围)?

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

html像这样:

<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 代码差不多是这样的:

........
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?'
........
........

现在我开始运行这个程序程序是运行。并且控制台正在打印价格,但仅两三分钟,我收到错误消息:

'IndexError: list index out of range'

然后程序中断了,我试了一遍又一遍。总是 'IndexError: list index out of range' 为什么没有告诉我 'price?' 或 'li?'。案例 BeautifulSoup?

表示您正在解析的页面没有id为market-summary
的元素 如果再次检查产生的错误,您可能会发现错误发生在第 1 行,即 if soup.select("#market-summary")[0] 或第 2 行,即 prices = soup.select("#market-summary")[0].findAll('li')