【发布时间】:2015-07-11 05:56:46
【问题描述】:
我搜索了这个问题并尝试使用上述解决方案,但似乎都没有工作。
我当前的代码是:
for item in g_data:
print item.contents[1].find_all("a", {"class": "a-link-normal"})[[1], [2], [3]]['href']
这导致TypeError: indices must be integers, not tuple。
我该如何解决这个问题?我知道这是一个简单的问题,但我尝试的解决方案导致它出现并说“not list”、“not str”或“not tuple”。
【问题讨论】:
-
尝试将 [ [1], [2], [3] ] 分成项目....find_all(....)[1] 然后 [2] 然后 [3]跨度>
-
g_data是什么类型? -
@RafaelCardoso 你的意思是像这样打印 item.contents[1].find_all("a", {"class": "a-link-normal"})[[1][2][ 3]]['href'] 这导致列表索引超出范围
-
一般来说,当这种情况发生时,尝试将它分布在多行上,这样你就可以看到确切的 TypeError 来自哪里。
-
如果你试图从列表中获取第二、第三和第四项,正确的语法不是
my_list[[1],[2],[3]],而是my_list[1:4]。如果您尝试从字典列表中的每个项目中获取href值,则正确的语法不是my_list_of_dicts["href"],而是[d["href"] for d in my_list_of_dicts]。
标签: python html integer web-scraping