jeudi 16 juillet 2020

Python Bs4 IF Statement with Print() function - 2 outcomes

I'm playing with IF statements with Bs4 to find a button class on a webpage, and can't for the life of me work out why the below isn't working as expected.

Desired outcome:

  • If the button class is present then the IF statement ends and the print() function prints ('This item is listed') so the script can continue running.

  • If the button is not present then the IF statement ends, the print() function prints ('This item is not listed') and the script can jump to some different code further down.

The script runs with no errors, but I can't get it to display the correct print() functions depending on if the button class is present or not. What am I doing wrong here?

Thank you

#look for the button class
def s_check():
    page = requests.get(URL, headers=headers)
    soup = BeautifulSoup(page.content, 'html.parser')
    sQuery = soup.findAll("div", {"class": "listed-btn"})
    if sQuery:
        return
    else:
        print('This item is NOT listed.')
print('This item is listed')

driver.get(URL)
time.sleep(2)

Aucun commentaire:

Enregistrer un commentaire