【问题标题】:How to fix "TypeError: 'int' object is not iterable" error in concurrent.futures threading?如何修复 concurrent.futures 线程中的“TypeError:'int' object is not iterable”错误?
【发布时间】:2019-09-21 06:00:33
【问题描述】:

我的目标是抓取一些链接并使用线程来更快地完成。

当我尝试创建线程时,它会引发TypeError: 'int' object is not iterable

这是我们的脚本:

import requests
import pandas
import json
import concurrent.futures
from from collections import Iterable

# our profiles that we will scrape
profile = ['kaid_329989584305166460858587','kaid_896965538702696832878421','kaid_1016087245179855929335360','kaid_107978685698667673890057','kaid_797178279095652336786972','kaid_1071597544417993409487377','kaid_635504323514339937071278','kaid_415838303653268882671828','kaid_176050803424226087137783']

# lists of the data that we are going to fill up with each profile
total_project_votes=[]

def scraper(kaid):
    data = requests.get('https://www.khanacademy.org/api/internal/user/scratchpads?casing=camel&kaid={}&sort=1&page=0&limit=40000&subject=all&lang=en&_=190425-1456-9243a2c09af3_1556290764747'.format(kaid))
    sum_votes=[]
    try:
        data=data.json()
        for item in data['scratchpads']:
            try :
                sum_votes=item['sumVotesIncremented']
            except KeyError:
                pass
        sum_votes=map(int,sum_votes) # change all items of the list in integers
        print(isinstance(sum_votes, Iterable)) #to check if it is an iterable element
        print(isinstance(sum_votes, int)) # to check if it is a int element
        sum_votes=list(sum_votes) # transform into a list
        sum_votes=map(abs,sum_votes) # change all items in absolute value
        sum_votes=list(sum_votes) # transform into a list
        sum_votes=sum(sum_votes) # sum all items in the list
        sum_votes=str(sum_votes) # transform into a string
        total_project_votes=sum_votes
    except json.decoder.JSONDecodeError:
        total_project_votes='NA'
    return total_project_votes

with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
    future_kaid = {executor.submit(scraper, kaid): kaid for kaid in profile}
    for future in concurrent.futures.as_completed(future_kaid):
        kaid = future_kaid[future]
        results = future.result()
        # print(results) why printing only one of them and then stops?
        total_project_votes.append(results[0])

# write into a dataframe and print it:
d = {'total_project_votes':total_project_votes}
dataframe = pandas.DataFrame(data=d)
print(dataframe)

我希望得到这个输出:

total_project_votes
0                   0
1                2353
2                  41
3                   0
4                   0
5                  12
6                5529
7                  NA
8                   2

但是我得到了这个错误:

TypeError: 'int' object is not iterable

我真的不明白这个错误是什么意思。我的脚本有什么问题?我该如何解决?

当我查看 Traceback 时,看起来问题出在以下位置: sum_votes=map(int,sum_votes).

下面有一些附加信息

追溯:

Traceback (most recent call last):
  File "toz.py", line 91, in <module>
    results = future.result()
  File "C:\Users\*\AppData\Local\Programs\Python\Python37-32\lib\concurrent\futures\_base.py", line 425, in result
    return self.__get_result()
  File "C:\Users\*\AppData\Local\Programs\Python\Python37-32\lib\concurrent\futures\_base.py", line 384, in __get_result
    raise self._exception
  File "C:\Users\*\AppData\Local\Programs\Python\Python37-32\lib\concurrent\futures\thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "my_scrap.py", line 71, in scraper
    sum_votes=map(int,sum_votes) # change all items of the list in integers
TypeError: 'int' object is not iterable

【问题讨论】:

  • 如果你查看整个错误信息,你会发现它发生在哪一行。
  • 您可能想查看分配给sum_votes 的值。您提供的每个代码示例都不同。
  • 嗯...你知道str(sum(list(map(abs,list(map(int,sum_votes)))))) 是做什么的吗?尝试将其拆分为多个语句并检查在哪个步骤中发生了什么。寻找应该是iterable,但实际上是int 的那一刻。
  • @zvone 编辑:我发现当我运行脚本时,有时我会得到一个可迭代元素 True。但对于一些我不知道的。有什么想法吗?
  • 当你得到int 然后检查你在 HTML 中的内容并在浏览器中查看这个页面。也许有些页面有不同的结构,你必须使用不同的代码来获取信息。或者服务器可能会发送警告说它不喜欢机器人和脚本。

标签: python python-3.x multithreading asynchronous web-scraping


【解决方案1】:

我发现了我的错误:

我应该说: sum_votes.append(item['sumVotesIncremented']) 代替: sum_votes=item['sumVotesIncremented'].

另外,因为我们这里只有一项:total_project_votes。我们的元组results 只有一项。 这可能会导致一些问题。因为当我们执行results[0] 时,它的行为不像列表。 它不会显示整个total_project_votes,而是显示字符串的第一个字符。 (例如“Hello”变成“H”)。 如果 total_project_votes 是一个 int 对象而不是一个字符串。它会产生另一个错误。 为了解决这个问题,我需要在元组results 中添加另一个对象,然后当您执行results[0] 时,它实际上就像一个列表。

【讨论】:

    猜你喜欢
    • 2020-02-03
    • 2022-12-15
    • 2018-06-29
    • 2022-11-14
    • 2019-04-12
    • 2016-02-18
    • 2022-11-02
    • 1970-01-01
    • 2020-11-07
    相关资源
    最近更新 更多