vendredi 29 septembre 2017

Python - Requests - If-Else statement prints out strangely?

So I have been playing around abit with request and it went good until I started to play around with "errors" which I mean if there is no name then it should print out no name..

However I come to this problem that whenever there is no name I get a traceback and also a really strange out prints which I can't find the logic of it.

As you can see below I have given exempels on what the program does with different code...

Input:

r = s.get(Url)
names = soup(r.text, 'html.parser')
findName = names.find('div', {'class':'nameslist'})

 if (r.status_code == 200):
    for name in names.find_all('option'):

        global nameID
        if myName == foundName:
            nameID = name.get('value')
            print('Profile-1 Found name')
        else:
            print("Did not find name")

    if (findName != None):
            notepresent = True
            print('There is a value')
        else:
            global addtonotes
            notepresent = False
            addtonotes = {'id': nameID}
            add2notes(addtonotes)   

Output:

Did not find name
Did not find name
Did not find name
Did not find name
Did not find name
Process Process-2:
Traceback (most recent call last):
  File "C:\Python\lib\multiprocessing\process.py", line 249, in _bootstrap
    self.run()
  File "C:\Python\lib\multiprocessing\process.py", line 93, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\Test.py", line 153
     addtonotes = {'id': nameID}
NameError: name 'nameID' is not defined
Did not find name
Did not find name
Did not find name
Did not find name
Did not find name
Did not find name
Did not find name
Profile-1 Found name
Did not find name
Did not find name
Did not find name
Did not find name
Did not find name

but then if i add sys.exit on

else:
            print("Did not find name")
            sys.exit()

then it will just say

Did not find name
Did not find name

even though the first one do find a name so it will just exit and say both are not finding...

BUT if I remove the else statement

then it will just give me the trackback meanwhile the Profile-1 prints out the same say.

As you can see what I want is if there is no name then it should just print out Did not find name once and then just sys.exit from there (Using multi-processing so the whole program wont end).

So I quite cant understand what is going on.. Maybe someone does find the issue?

My wish print is:

(If it finds name)

profile-1 find name <---- Profile 1 etc
Did not find name  <--- Profile 2 etc

and then continue the code.

What could possible go wrong? (Feel free to comment if anything missing or missunderstood)

Aucun commentaire:

Enregistrer un commentaire