Python-IndexError:列表索引超出范围-不起作用 [英] Python - IndexError: list index out of range - not working

查看:40
本文介绍了Python-IndexError:列表索引超出范围-不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的scrap.py代码

This is my scrap.py code

from bs4 import BeautifulSoup as soup
from urllib.request import urlopen as uReq

website = "https://houston.craigslist.org/search/cta"

uClient = uReq(website)
page_html = uClient.read()
uClient.close()

soup_html = soup(page_html, "html.parser")
result_html = soup_html.findAll("p", {"class":"result-info"})

filename = "products.csv"

f = open(filename, "w", encoding='utf8')
headers = "car_name, price\n"
f.write(headers)

for container in result_html:   
    carname = container.a.text

    price_container = container.findAll('span', {'class':'result-price'})
    price = price_container[0].text

    f.write(carname + "," + price + "\n")

f.close()

在终端上,它工作正常,但是当我循环它时,会出现以下错误.

On terminal, it works fine however when I loop it, it gives the following error..

Traceback (most recent call last):
  File "scrap.py", line 23, in <module>
    price = price_container[0].text.splitlines()
IndexError: list index out of range

请帮助.谢谢

推荐答案

尝试以下方法.它将获取所有项目和价格,并处理 IndexError (如果有).

Try the below one. It will fetch you all the items and price and handle IndexError if there is any.

from bs4 import BeautifulSoup
from urllib.request import urlopen

response = urlopen("https://houston.craigslist.org/search/cta")
soup_html = BeautifulSoup(response.read(), "html.parser")
for container in soup_html.find_all("p", {"class":"result-info"}):   
    carname = container.find_all("a")[0].text
    try:
        price = container.find_all('span', {'class':'result-price'})[0].text
    except IndexError:
        price = ""
    print(carname,price)

我试图缩短您的代码以使其看起来更好.

I tried to shorten your code to make it look better.

这篇关于Python-IndexError:列表索引超出范围-不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆