如何避免"列出索引超出范围"在BeautifulSoup(Python)

时间:2016-07-12 09:58:56

标签: python beautifulsoup python-2.x

执行此代码时,我总是收到错误消息:" IndexError:列表索引超出范围"。我该怎么做才能避免这种情况?

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

1 个答案:

答案 0 :(得分:0)

您错过了检查列表是否为空的检查。试试这个

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