【问题标题】:Python 3.5.2 web-scraping - list index out of rangePython 3.5.2 web-scraping - 列表索引超出范围
【发布时间】:2016-09-20 17:51:06
【问题描述】:

我是新的网络抓取,并试图抓取餐厅详细信息表单的所有内容,以便我可以继续进行进一步的抓取。

import requests
from bs4 import BeautifulSoup
import urllib

url = "https://www.foodpanda.in/restaurants"
r=requests.get(url)
soup=BeautifulSoup(r.content,"html.parser")
print(soup.find_all("Section",class_="js-infscroll-load-more-here")[0])

【问题讨论】:

  • 问题是什么?
  • 我收到 IndexError: list index out of range as error
  • 通过回溯发布您对问题的完整解释。如果您收到该错误,则意味着 soup.find_all("Section",class_="js-infscroll-load-more-here") 是一个空列表。
  • 你能发布整个错误信息吗?我怀疑问题出在 findall(...)[0] 语句中。如果 findall 返回一个空列表,那么您对索引 0 的调用超出了范围。
  • 尝试获取餐厅表单的所有内容,但我在 Traceback 中遇到错误(最近一次调用最后一次):文件“C:\Users\aenis\Desktop\zomotaScript.py”,第 7 行,在 print(soup.find_all("Section",class_="js-infscroll-load-more-here")[0]) IndexError: list index out of range [Finished in 6.2s]

标签: python web-scraping beautifulsoup


【解决方案1】:

问题在于为soup.find_all("Section",class_="js-infscroll-load-more-here"‌​) 访问索引0 处的元素,因为结果是一个空列表。

【讨论】:

    【解决方案2】:

    html 没有大写标签的概念,即使在源代码本身中,它也是 section 而不是带有小写 s 的 Section

    section = soup.find_all("section",class_="js-infscroll-load-more-here")[0]
    

    既然只有一个,你也可以使用 find:

     section = soup.find("section",class_="js-infscroll-load-more-here")
    

    两者都会找到您要查找的内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多