简体   繁体   English

如何修复多处理器 TypeError: 'list' object is not callable

[英]How to fix a multiprocessor TypeError: 'list' object is not callable

This code iterates through a list to web scrape synonyms on www.thesaurus.com.... I divide it into different multi processors to make it faster.这段代码通过一个列表迭代到 web 在 www.thesaurus.com 上抓取同义词....我将它分成不同的多处理器以使其更快。

def pri():
    return emotions
ok = pri()
skipped =  []
def emo(numb, max):
    while numb <= max:
        try:
            words = ok[numb]
            URL = 'https://www.thesaurus.com/browse/'+words.word+"?s=t"
            page = requests.get(URL)
            soup = BeautifulSoup(page.content,'html.parser')
            result = soup.find(class_="css-11nwwws e1gu66k41")
            try:
                n = result.text
                nws = n.split('S')
                nwn = nws[1].split('N') 
                lonwn = len(nwn[0])
                more =[]
                repeat = False
                for i in nwn[0]:
                    trial = nwn[0]
                    total = len(nwn[0])
                    if i not in more:
                        more.append(i)
                    elif i == trial[total-1:]:
                        repeat = False
                        break
                    else:
                        repeat =True
                        break
                if repeat == True:
                    la = lonwn-2
                    gnwn = nwn[0]
                    number = int(gnwn[la:])
                else:
                    la = lonwn-1
                    gnwn =nwn[0]
                    number = int(gnwn[la:])
                    while number != 0:
                        if not ConnectionError == True:
                            URL = 'https://www.thesaurus.com/browse/'+words.word+"?s=t"
                            page = requests.get(URL)
                            soup = BeautifulSoup(page.content,'html.parser')
                            result = soup.find(class_="css-11nwwws e1gu66k41")
                            try:
                                definition = soup.find_all("a",class_="css-1wndipq eh475bn1")
                                for define in definition:
                                    if define.text not in words.array:
                                        emotions[numb].array.append(define.text)
                                    else:
                                        continue

                            except AttributeError:
                                URL ='https://www.thesaurus.com/browse/'+words.word+'/'+str(number)
                                page= requests.get(URL)
                                soup = BeautifulSoup(page.content,'html.parser')
                                try:
                                    definition = soup.find_all("a",class_="css-1wndipq eh475bn1")
                                    for define in definition:
                                        if define.text not in words.array:
                                            emotions[numb].array.append(define.text)
                                        else:
                                            continue
                                except AttributeError:
                                    pass
                        else:
                            print("Connection error could not get emotion", emotions[numb].word)
                        number-=1
                        URL ='https://www.thesaurus.com/browse/'+emotions[numb].word+'/'+str(number)
                        if number <=1:
                            break
            except AttributeError:
                error = emotions[numb].word
                skipped.append(error)
                pass
        except TypeError:
            pass
        numb +=1
        
p = Process(target=emo, args=(0,50))
p6 = Process(target=emo, args=(51,100))
p1 = Process(target=emo, args=(101,150))
p2 = Process(target=emo, args=(151,200))
p3 = Process(target=emo, args=(201,251))
p4 = Process(target=emo, args=(251,300))
p5 = Process(target=emotions, args=(301,321))
if __name__ == "__main__":
    print("Training Bot, please wait")
    p.start()
    p6.start()
    p1.start()
    p2.start()
    p3.start()
    p4.start()
    p5.start()
    p.join()
    p1.join()
    p2.join()
    p3.join()
    p4.join()
    p6.join()
    p5.join()
    if len(skipped) >0:
        print("Sadly the computer could not get the right definition and synonyms for the some emotions, would you like to help (y/n)\n")
        ask = input("Me:")
        if ask.lower == "y":
            where = 0
            for i in skipped:
                for emo in emotions:
                    where+=1
                    if i == emo:
                        break
                print("Please go to " + 'https://www.thesaurus.com/browse/'+emotions[where]  + ". and copy and paste all the synonyms for" + i)
                print("Please be careful because that will be how we train the bot.")
                print("input list\n")
                ask = input("Me: ")
                for li in ask:
                    emotions[where].array.append(li)
    print("Done training Bot")

    print("Welcome to Advice Bot. Program will start and when ever your ready for it to end. type quit.")
    cont = True
    print("How have you been lately?")

But while running this happened:但是在运行时发生了这种情况:

Training Bot, please wait
Process Process-7:
Traceback (most recent call last):
  File "C:\Users\Nnaji\AppData\Local\Programs\Python\Python39\lib\multiprocessing\process.py", line 315, in _bootstrap
    self.run()
  File "C:\Users\Nnaji\AppData\Local\Programs\Python\Python39\lib\multiprocessing\process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
TypeError: 'list' object is not callable

I tried adding a try/except TypeError to see if that will catch and then move on, without pausing the program.我尝试添加一个try/except TypeError来查看它是否会捕获然后继续,而不暂停程序。 But that didn't work.但这没有用。 I got no more ideas.我没有更多的想法。

Edit Turns out I made the mistake of putting emotions instead of emo, so the computer was trying to call the list emotions, instead of the function emo编辑结果我犯了一个错误,把情绪而不是情绪,所以电脑试图调用列表情绪,而不是 function 情绪

replace代替

p5 = Process(target=emotions, args=(301,321))

with

p5 = Process(target=emo,args=(301,321))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM