0
我正在構建一個簡單的網頁抓取腳本來讓我的腳溼蟒蛇。 我打了一點,但有以下追加到列表中的問題 - IndexError:列表索引超出範圍
#Create 3 different lists to populate.
mails = []
phones = []
webs = []
def go_get_info(info):
for item in info:
#email = (item.contents[3].find_all("span", {"class": "text"})[0].text).strip()
#phone = (item.contents[3].find_all("span", {"class": "text"})[1].text).strip()
www = (item.contents[3].find_all("span", {"class": "text"})[2].text).strip()
if not www:
webs.append("empty")
else:
webs.append(www)
問題的
的想法是,我會得到電子郵件,電話和網址爲每三個列表,壓縮它們togther,然後遍歷和寫入CSV。
在這裏,我似乎有一個問題唯一的價值是www **(所以你可以看到我已經留下了它未註釋)。 **我也嘗試通過添加一個空的條件,以減輕這個問題。****
當我運行調用此函數的腳本,我回到了以下
± |add-csv-support U:1 ?:1 ✗| → python scrape.py
Traceback (most recent call last):
File "scrape.py", line 55, in <module>
go_retrieve_contact(get_venue_link_list(links))
File "scrape.py", line 30, in go_retrieve_contact
go_get_info(info)
File "scrape.py", line 43, in go_get_info
www = (item.contents[3].find_all("span", {"class": "text"})[2].text).strip()
IndexError: list index out of range
這是有道理的對我來說,問題是價值被返還或缺乏價值。我GOOGLE了,但找不到完整的解決方案。
什麼可能我在這種情況下做
A)更好地瞭解發生了什麼,並調試好。
B)解決問題。
謝謝,
我想'線find_all'返回一個匹配列表。如果索引2超出範圍,那意味着它找到少於3個匹配。將結果保存到變量中,然後在嘗試訪問不存在的元素之前檢查長度*。然後你可以決定你想如何處理這種情況。 –