我无法通过“列表索引超出范围”错误消​​息

时间:2019-08-21 12:39:13

标签: python beautifulsoup screen-scraping

我无法通过“ IndexError:列表索引超出范围”错误消​​息。以下是我的代码,并且我知道'variant'对象并不总能找到结果,因为它可能不存在,但是我如何停止它来打破循环并继续搜索呢,只打印找到的结果。

我已经尝试过if语句,但是我一定做错了。

import requests

from bs4 import BeautifulSoup

url = 'https://www.glenmarch.com/cars/results?make&model&auction_house_id&auction_location&year_start&year_end&low_price&high_price&auction_id&fromDate=01%2F08%2F2018&toDate=01%2F08%2F2019&keywords&show_unsold_cars=0&limit=100'

get_url = requests.get(url)

get_text = get_url.text

soup = BeautifulSoup(get_text, 'html.parser')

car_listing = soup.findAll("div", {"class": "car-item"})

for cars in car_listing:
    car = cars.find('div', 'make').text
    title = car.split()
    year = title[0]
    make = title[1]
    model = title[2]
    variant = title[3]
    price = cars.find('div', 'price').text
    print(year, make, model, variant, price)

我期望得到一个'variant'结果,但是相反,它会不断破坏不存在结果的代码。

4 个答案:

答案 0 :(得分:0)

要避免错误,我们可以使用try catch语句。这将等待错误,然后在发现错误时运行catch语句。

答案 1 :(得分:0)

在答案旁边,您有两种选择,请检查长度或接受是否有错误并忽略它。

if len(title)>3:
    year = title[0]
    make = title[1]
    model = title[2]
    variant = title[3]

#or try this
try:
    year = title[0]
    make = title[1]
    model = title[2]
    variant = title[3]
except:
    pass

答案 2 :(得分:0)

使用try...except语句。像这样使用它:

try:
    '''code in which there is possibly an error'''
except:
    '''what you want to do if there is an error'''

try检查是否有错误,如果发生错误,except会按照您的指示进行操作。

答案 3 :(得分:0)

一种可能性是在混凝土拍卖行中废弃此信息。该脚本将搜索到silverstoneauctions.com的所有链接,并从那里获取信息。对于其他拍卖行,则必须更改代码。

import requests
from bs4 import BeautifulSoup

url = 'https://www.glenmarch.com/cars/results?make&model&auction_house_id&auction_location&year_start&year_end&low_price&high_price&auction_id&fromDate=01%2F08%2F2018&toDate=01%2F08%2F2019&keywords&show_unsold_cars=0&limit=100'

soup = BeautifulSoup(requests.get(url).text, 'html.parser')

for car in soup.select('[data-type="car_grid_item"]'):
    a = car.select_one('a[href*="silverstoneauctions.com"]')
    if not a:
        continue

    s = BeautifulSoup(requests.get(a['href']).text, 'html.parser')

    make = s.select_one('.lot__specification--label:contains("Make:") + div')
    print('Make:', make.text if make else '')

    year = s.select_one('.lot__specification--label:contains("Year of manufacture:") + div')
    print('Year:', year.text if year else '')

    model = s.select_one('.lot__specification--label:contains("Model:") + div')
    print('Model:', model.text if model else '')

    price = car.select_one('.price')
    print('Price:', price.text if price else '')

    print('-' * 80)

打印:

Make: Lancia
Year: 1972
Model: Fulvia
Price: £39,375
--------------------------------------------------------------------------------
Make: Austin
Year: 1963
Model: Mini Cooper S
Price: £65,250
--------------------------------------------------------------------------------
Make: Bentley
Year: 1997
Model: Continental R
Price: £39,375
--------------------------------------------------------------------------------
Make: BMW
Year: 1990
Model: 325i Convertible
Price: £42,188
--------------------------------------------------------------------------------

...and so on.