索引器:在使用beautifulsoup创建广告时,列表索引超出范围

2024-09-26 22:55:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在我的本地网站上搜索公寓购买/租赁广告。你知道吗

在某些情况下,我收到IndexError:list索引超出范围错误。你知道吗

当我的scraper遇到一个没有某些参数的add时,我收到了错误。通常是Povierzchnia(大小),Liczba pokoi(房间数量),Pietro(楼层),Rok budowy(建成年份-我不刮)

我想是因为这个:

pietro = ogl.find_all('p', class_ ="list__item__details__icons__element__desc")[2].text 

如果没有[2],这通常是第三个参数,它会抛出一个错误,说明这个[2]超出了范围。你知道吗

我试着在for循环中输入if,检查是否有这样的参数,如果没有,继续。但是我没能挺过去。你知道吗

我也试着这样使用它:

Powierzchnia = zrzut.find_all('li', class_ = "list__item__details__icons__element details--icons--element--powierzchnia")[0].text

这个没有抛出错误,但是给了所有addvertisments相同的大小

完整代码如下:

from bs4 import BeautifulSoup
from requests import get
import pandas as pd
import itertools
import matplotlib.pyplot as plt


headers = ({'User-Agent':
            'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'})
link = 'https://ogloszenia.trojmiasto.pl/nieruchomosci/wi,100,dw,1d.html?' + str(strona)
r = get(link, headers = headers)
zupa = BeautifulSoup(r.text, 'html.parser')

ogloszenia= zupa.find_all('div', class_="list__item")

n_stron = 0
numer = 0
for strona in range(0,12):
    n_stron +=1
    for ogl in ogloszenia:
        tytul = ogl.find_all('h2', class_ ="list__item__content__title")[0].text
        powierzchnia = ogl.find_all('p', class_ ="list__item__details__icons__element__desc")[0].text
        liczba_pokoi = ogl.find_all('p', class_ ="list__item__details__icons__element__desc")[1].text
        pietro = ogl.find_all('p', class_ ="list__item__details__icons__element__desc")[2].text
        lokalizacja = ogl.find_all('p', class_ = "list__item__content__subtitle")[0].text
        cena = ogl.find_all('p', class_ = "list__item__price__value")[0].text
        cena_m = ogl.find_all('p', class_ = "list__item__details__info details--info--price")[0].text

        numer += 1
        print(numer)
        print(tytul)
        print('Powierzchnia: ' + powierzchnia )
        print('Lokalizacja: ' + lokalizacja )
        print('Liczba pokoi: ' + liczba_pokoi )
        print('Pietro: ' + pietro )
        print('Cena: ' + cena )
        print('Cena za metr kwadratowy: ' + cena_m +'\n')

Tags: textimport错误elementallfinddetailsitem
3条回答

您可以捕获IndexError异常并将变量设置为None''

try:
    powierzchnia = ogl.find_all('p', class_ ="list__item__details__icons__element__desc")[0].text
except IndexError:
    powierzchnia = ''

对于其他变量也可能会遇到这种情况。对每个人重复同样的步骤。你知道吗

尝试:

data = ogl.find_all('p', class_ ="list__item__details__icons__element__desc")
for idx,entry in enumerate(data):
    if idx == 0:
        print('powierzchnia {}'.format(entry.text))
    elif idx == 1:
        print('liczba_pokoi {}'.format(entry.text))
    else:
        print('pietro {}'.format(entry.text))

我建议做两个改变。你知道吗

首先,尝试隔离函数中的重复命令。你知道吗

def findDetail(ogl, tag, class, index):
     return ogl.find_all(tag, class_ = class)[index].text

然后,在索引不可用的情况下,可以使用“try except”来处理它。这是处理Python中错误的标准方法:

def findDetail(ogl, tag, class, index):
    try:
        return ogl.find_all(tag, class_ = class)[index].text
    except IndexError:
        print(f”Could not find index {index} for {tag} with {class}”)
        return “”

那就叫它:

for ogl in ogloszenia:
    tytul = findDetail(ogl, “h2”, “"list__item__content__title", 0)
    powierzchnia = findDetail(ogl, ‘p’, "list__item__details__icons__element__desc", 0)

等等。如果找不到索引,则只打印一个空白字符串。你知道吗

相关问题 更多 >

    热门问题