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.
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. 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
replace
p5 = Process(target=emotions, args=(301,321))
with
p5 = Process(target=emo,args=(301,321))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.