网页抓取错误

时间:2017-02-15 14:54:02

标签: python

我在下面的代码中从网站上提取了一些数据,但是我遇到了这一行duration = tr.select('td.duration')[0].contents[0].strip()中的持续时间的问题,这引发了下面的异常。请问我该如何修复该行谢谢你为了提取持续时间数据。我在SO上搜索了类似的问题,但他们并没有完全回答我的问题。

# import needed libraries
from mechanize import Browser
from bs4 import BeautifulSoup
import csv

br = Browser()

# Ignore robots.txt
br.set_handle_robots(False)
br.addheaders = [('User-agent', 'Chrome')]

# Retrieve the home page
br.open('http://fahrplan.sbb.ch/bin/query.exe/en')
br.select_form(nr=6)

br.form["REQ0JourneyStopsS0G"] = 'Eisenstadt'  # Origin train station (From)
br.form["REQ0JourneyStopsZ0G"] = 'sarajevo'  # Destination train station (To)
br.form["REQ0JourneyTime"] = '5:30'  # Search Time
br.form["date"] = '18.01.17'  # Search Date

# Get the search results
br.submit()

# get the response from mechanize Browser
soup = BeautifulSoup(br.response().read(), 'lxml', from_encoding="utf-8")
trs = soup.select('table.hfs_overview tr')

# scrape the contents of the table to csv (This is not complete as I cannot write the duration column to the csv)
with open('out.csv', 'w') as f:
    for tr in trs:
        locations = tr.select('td.location')
        if len(locations) > 0:
            location = locations[0].contents[0].strip()
            prefix = tr.select('td.prefix')[0].contents[0].strip()
            time = tr.select('td.time')[0].contents[0].strip()
            duration = tr.select('td.duration')[0].contents[0].strip()
            f.write("{},{},{},{}\n".format(location.encode('utf-8'), prefix, time, duration))

Traceback (most recent call last):
  File "C:/.../tester.py", line 204, in <module>
    duration = tr.select('td.duration')[0].contents[0].strip()
IndexError: list index out of range

Process finished with exit code 1

1 个答案:

答案 0 :(得分:1)

tr.select('td.duration')是长度为零的列表,或tr.select('td.duration')[0].contents是长度为零的列表。你需要以某种方式防范这些可能性。一种方法是使用条件。

durations = tr.select('td.duration')
if len(durations) == 0:
    print("oops! There aren't any durations.")
else:
    contents = durations[0].contents
    if len(contents) == 0:
        print("oops! There aren't any contents.")
    else:
        duration = contents[0].strip()
        #rest of code goes here

或许您可能只是忽略那些不适合您预期模型的TR,在这种情况下,尝试捕获可能就足够了。

try:
    duration = tr.select('td.duration')[0].contents[0].strip()
except IndexError:
    print("Oops! tr didn't have expected tds and/or contents.")
    continue
#rest of code goes here