如何在python中跳过TypeError、AttributeError和IndexError进行迭代



我试图使用BeautifulSoup从维基百科表中获取信息。现在我是堆叠的,因为我不能循环通过一个对象。

这是代码:

import requests
from bs4 import BeautifulSoup
url='http://de.wikipedia.org/wiki/Liste_der_in_der_Europ%C3%A4ischen_Union_zugelassenen_Lebensmittelzusatzstoffe'
raw_data=requests.get(url)
soup=BeautifulSoup(raw_data.content)
table= soup.find_all("table",{"class":"wikitable sortable"})
for i in table:
    print i.contents[i].find_all("td")

这就是错误:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
TypeError: list indices must be integers, not Tag

如果我使用try:和except:,则不会打印任何内容。

有人能帮我吗?

非常感谢!

你是对的,你可以迭代跳过TypeErrorAttributeErrorIndexError,通常处理异常。

然而,这里的错误是:

TypeError: list indices must be integers, not Tag

这个错误是由引起的

i.contents[i]

这里i不是一个整数,而是一个beautifulsoup元素标记。因此,这样就无法为列表编制索引。

>>> type(i)
<class 'bs4.element.Tag'>

因此,我相信您正在尝试查找其中的所有td标记。现在,当您在table上循环时,您已经选择了该元素。因此,只需执行find_all就可以获得所有td元素:

i.find_all("td")

所以,你的代码应该是:

import requests
from bs4 import BeautifulSoup
url = 'http://de.wikipedia.org/wiki/Liste_der_in_der_Europ%C3%A4ischen_Union_zugelassenen_Lebensmittelzusatzstoffe'
raw_data = requests.get(url)
soup = BeautifulSoup(raw_data.content)
table = soup.find_all("table", {"class": "wikitable sortable"})
for i in table:
    print i.find_all("td")

当您编写时

i.contents[i].find_all("td")

,你到底期望i是什么?你期望i.contents是什么?为什么您希望能够使用i本身作为i.contents的索引?

您可能应该回去更仔细地阅读文档,并确切地了解soup.find_all返回的内容,这样您就可以了解迭代的i值是什么。

for i in table:
    print i.contents[i].find_all("td")

这里,i是列表中的元素-table;不是整数我们不能做contents[i]

你可能想试试这样的东西,

import requests
from bs4 import BeautifulSoup
url='http://de.wikipedia.org/wiki/Liste_der_in_der_Europ%C3%A4ischen_Union_zugelassenen_Lebensmittelzusatzstoffe'
raw_data=requests.get(url)
soup=BeautifulSoup(raw_data.content)
table= soup.find_all("table",{"class":"wikitable sortable"})
for i in table:
    print i.find_all("td")

以下是您的个性化答案:-),

for i in table:
    for c in i.contents:
        try:print c.find_all("td")
        except:pass

希望能有所帮助:-)

最新更新