【发布时间】:2020-04-23 09:48:21
【问题描述】:
请帮我解决这个错误:
Traceback (most recent call last):
File "D:/Python/parser.py", line 14, in <module>
for i in title[0].text:
IndexError: list index out of range
代码:
import requests
from bs4 import BeautifulSoup as BS
max_page = 6
pages = []
for x in range(1, max_page + 1):
pages.append( requests.get("https://stopgame.ru/review/new/stopchoice/p" + str(x) ) )
for r in pages:
html = BS(r.content, "html.parser")
for el in html.select(".lent-block"):
title = el.select(".lent-block > a")
for i in title[0].text:
title.text[0](title)
【问题讨论】:
-
那说明你的标题列表是空的,为什么不检查
title的内容 -
欢迎来到 SO!
title = el.select(".lent-block > a")返回一个空列表,因此[][0]超出范围。你的预期输出是什么?您是否尝试获取标题文本?
标签: python beautifulsoup