Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unexpected output when I use BeautifulSoup for web scraping

I don’t know why my checkTargetExist function is returning the unexpected result.

from bs4 import BeautifulSoup

def checkTargetExist(soup, id):
    errorDiv = soup.find(id)
    #if there is <div> which informs error message then my target does not exist.
    if errorDiv:
        targetExist = False
    else:
        targetExist = True
    return targetExist

# Create a BeautifulSoup object from your HTML content
html_content = """
<html>
  <body>
    <div id="errorDiv">We cannot find your search.</div>
  </body>
</html>
"""
soup = BeautifulSoup(html_content, 'lxml')

# Call the function to check if my target exists.
result = checkTargetExist(soup, "errorDiv")

# Print the result
print(result)

The code should print out False, but I get True from this code. Am I wrong?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Take a look in the documentation:

https://www.crummy.com/software/BeautifulSoup/bs4/doc/#find-all

# is searching for <errorDiv></errorDiv>
errorDiv = soup.find(id)

# is searching for <tag id="errorDiv"></tag>
errorDiv = soup.find(id='errorDiv')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading