2016-04-03 34 views
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)解決問題。

謝謝,

+0

我想'線find_all'返回一個匹配列表。如果索引2超出範圍,那意味着它找到少於3個匹配。將結果保存到變量中,然後在嘗試訪問不存在的元素之前檢查長度*。然後你可以決定你想如何處理這種情況。 –

回答

1

的問題是,你是指第四元件(item.contents[3])或第三元件(find_all(...)[2])和那些2個陣列中的一個不具有許多元素,這是list index out of range手段。

www = (item.contents[3].find_all("span", {"class": "text"})[2].text).strip() 

因爲這是一個刮削工具的一部分,你可能要編寫檢查多少元素你在你的find_all通過嵌套它在if len((...).find_all(...)) >= 3語句或使用越來越try except

相關問題