【发布时间】:2017-11-18 02:50:29
【问题描述】:
我正在尝试从 airport website 中提取包含航班到达信息的表格(包含列 - 航班、承运人、出发地、日期、计划、估计、状态),但出现以下错误:
IndexError Traceback (most recent call last)
<ipython-input-39-2f7369a95ba9> in <module>()
6 for cl in cols:
7 dv = cl.findAll('div')
----> 8 if 'col-xs-12 col-sm-6' in dv[0]['class']:
9 flight, carrier, origin, date, scheduled, estimated, status = [c.text for c in dv]
10 print(flight, carrier, origin, date, scheduled, estimated, status)
IndexError: list index out of range
我已经梳理了 stackoverflow 的解决方案,但找不到解决方案。这是我的代码:
# import libraries
import urllib3
import requests
from bs4 import BeautifulSoup
# query the website and return the html to the variable ‘page’
page = requests.get("https://www.aucklandairport.co.nz/flights").text
soup = BeautifulSoup(page)
tbody = soup.findAll('tbody')
for tb in tbody:
rows = tb.findAll('tr')
for tr in rows:
cols = tr.findAll('td')
for cl in cols:
dv = cl.findAll('div')
if 'col-xs-12 col-sm-6' in dv[0]['class']:
flight, carrier, origin, date, scheduled, estimated, status = [c.text for c in dv]
print(flight, carrier, origin, date, scheduled, estimated, status)
感谢您的贡献。
【问题讨论】:
标签: python python-3.x web-scraping beautifulsoup