如何避免;列表索引超出范围“;在BeautifulSoup(Python)中
执行此代码时,我总是收到错误消息:“IndexError:list index超出范围”。我能做些什么来避免这种情况如何避免;列表索引超出范围“;在BeautifulSoup(Python)中,python,beautifulsoup,python-2.x,Python,Beautifulsoup,Python 2.x,执行此代码时,我总是收到错误消息:“IndexError:list index超出范围”。我能做些什么来避免这种情况 import urllib thisurl = "http://www.tutti.ch/stgallen/fahrzeuge/autos" handle = urllib.urlopen(thisurl) html_gunk = handle.read() from bs4 import BeautifulSoup soup = BeautifulSoup(html_gunk
import urllib
thisurl = "http://www.tutti.ch/stgallen/fahrzeuge/autos"
handle = urllib.urlopen(thisurl)
html_gunk = handle.read()
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_gunk, 'html.parser')
for first in soup.find_all("li", class_="li-in"):
#print first.select("em.fl.in-date")
if (first.select("em.fl.in-date")[0].text is not None):
time = first.select("em.fl.in-date")[0].text
if (len(time) > 0):
if first.select("em.fl.in-date")[0].text[6] == "H":
zeit = first.select("em.fl.in-date")[0].text[11:16]
print zeit
if first.select("em.fl.in-date")[0].text[6] == "G":
zeit = first.select("em.fl.in-date")[0].text[13:18]
print zeit
您缺少检查列表是否为空的检查。试试这个
import urllib
thisurl = "http://www.tutti.ch/stgallen/fahrzeuge/autos"
handle = urllib.urlopen(thisurl)
html_gunk = handle.read()
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_gunk, 'html.parser')
for first in soup.find_all("li", class_="li-in"):
#print first.select("em.fl.in-date")
if first.select("em.fl.in-date") is not None and len(first.select("em.fl.in-date")) > 0:
if (first.select("em.fl.in-date")[0].text is not None):
time = first.select("em.fl.in-date")[0].text
if (len(time) > 0):
if first.select("em.fl.in-date")[0].text[6] == "H":
zeit = first.select("em.fl.in-date")[0].text[11:16]
print zeit
if first.select("em.fl.in-date")[0].text[6] == "G":
zeit = first.select("em.fl.in-date")[0].text[13:18]
print zeit
空列表的第一个(
seq[0]
)元素是什么?不要做同样的选择两次,将其存储为变量并使用它