像这样的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:列表索引超出范围'
程序中断,我一次又一次地尝试。总是'IndexError: list index out of range'
为什么没有显示“价格”?或“李?”。案例BeautifulSoup?