Issue
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?
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')
Answered By - Shirony
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.