python爬蟲中的“list index out of range”錯誤:原因及解決方法
在使用Python和beautifulsoup進行網頁爬取時,經常會遇到list index out of range錯誤。即使代碼沒有修改,也可能出現這種問題,尤其是在處理動態網頁或網站結構變化時。本文將分析此錯誤的原因,并提供有效的解決方案。
以下是一個示例代碼,它演示了可能導致該錯誤出現的情況:
import requests from bs4 import BeautifulSoup headers = {'user-agent': 'Mozilla/5.0 (windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0'} response = requests.get("https://www.iqiyi.com/ranks1/3/0", headers=headers) print(response.status_code) response = response.text soup = BeautifulSoup(response, "html.parser") def extract_data(): titles = [title.get_text().strip() for title in soup.find_all("div", class_="rvi__tit1")] heat = [heat.get_text().strip() for heat in soup.find_all("span", class_="rvi__index__num")] introductions = [intro.get_text().strip() for intro in soup.find_all("p", class_="rvi__des2")] return titles, heat, introductions def display_data(titles, heat, introductions): min_len = min(len(titles), len(heat), len(introductions)) for i in range(min_len): print(f"排名: {i+1}, 標題: {titles[i]}, 熱度: {heat[i]}, 簡介: {introductions[i]}") if __name__ == '__main__': titles, heat, introductions = extract_data() display_data(titles, heat, introductions)
在這個例子中,list index out of range錯誤通常發生在display_data函數中。原因是:titles,heat,introductions這三個列表的長度可能不一致。如果其中一個列表的長度小于10(或者循環的范圍),那么在訪問列表元素時就會出現索引越界錯誤。
解決方法:
立即學習“Python免費學習筆記(深入)”;
關鍵在于確保在訪問列表元素之前,檢查列表的長度,并只訪問有效索引范圍內的元素。 改進后的代碼如下:
import requests from bs4 import BeautifulSoup # ... (headers and request remain the same) ... def extract_data(): # ... (extraction remains the same) ... def display_data(titles, heat, introductions): min_len = min(len(titles), len(heat), len(introductions)) # Find the shortest list for i in range(min_len): print(f"排名: {i+1}, 標題: {titles[i]}, 熱度: {heat[i]}, 簡介: {introductions[i]}") if __name__ == '__main__': titles, heat, introductions = extract_data() display_data(titles, heat, introductions)
通過計算三個列表中最短的長度 min_len,并使用 min_len 作為循環的范圍,我們確保了不會訪問到任何超出列表索引范圍的元素,從而有效地避免了list index out of range錯誤。 這是一種更健壯的處理方式,能夠適應不同網頁結構和數據數量的變化。 此外,添加錯誤處理機制(例如try-except塊)也是一種好的編程實踐,可以處理更復雜的情況。
? 版權聲明
文章版權歸作者所有,未經允許請勿轉載。
THE END
喜歡就支持一下吧
相關推薦