This question already has an answer here:
Beautiful Soup 4 find_all don't find links that Beautiful Soup 3 finds
                                
                                    (1个答案)
                                
                        
                                5年前关闭。
            
                    
我的代码如下所示。

from bs4 import BeautifulSoup
import requests

r  = requests.get("http://www.data.com.sg/iCurrentLaunch.jsp")
data = r.text
soup = BeautifulSoup(data)
n = soup.findAll('table')[7].findAll('table')
for tab in n:
    print tab.findAll('td')[1].text


我得到的是直到IDYLLIC SUITES的属性名称,之后出现错误“列表索引超出范围”。这是什么问题?

最佳答案

我不确定到底是什么困扰着你。因为当我尝试您的代码(按原样)时,它对我有用。

尽管如此,尝试更改解析器,可能是html5lib

也是

点安装html5lib

然后将您的代码更改为

from bs4 import BeautifulSoup
import requests

r  = requests.get("http://www.data.com.sg/iCurrentLaunch.jsp")
data = r.text
soup = BeautifulSoup(data,'html5lib') # Change of Parser
n = soup.findAll('table')[7].findAll('table')
for tab in n:
    print tab.findAll('td')[1].text


让我知道是否有帮助

10-06 01:44