-2

I'm currently learning Python to do some webscraping and I'm facing with an issue with If and Else

The current code is the following :

r= requests.get(urlpage, headers=headers)
soup = BeautifulSoup(r.text, 'html.parser')
div = soup.find('div', attrs={'class':'online'})

result = print(div.string)

if result == 'Online':
    print('Ok')
else:
    print('Next Time')

I have written print(div.string) to get rid of div elements which is : <div class="online">Online</div> so I get "Online" only which is working

but when I try if and else I always get as result "Next Time" while I'm supposed to get "Ok".

At this point i'm kinda lost, do you have any idea what's wrong ?

Oxykore
  • 53
  • 6

1 Answers1

1

just get string directly, no need for print

also use lower function, makes it easier to compare

r= requests.get(urlpage, headers=headers)
soup = BeautifulSoup(r.text, 'html.parser')
div = soup.find('div', attrs={'class':'online'})

result = div.string # just get string directly, no need for print

if lower(result) == 'online':
    print('Ok')
else:
    print('Next Time')
Dean Van Greunen
  • 5,060
  • 2
  • 14
  • 28