Beautiful Soup Assistance

Hello, I am very new to Python. Please forgive my considerable ignorance. I have borrowed some code and modified it to meet my requirements. However, I am not getting the expected result.

Expected Result:
If the assigned URL contains the word “Wine” 1 time on the website, wait 15 second, loop back and start the code over again.
Else, print message.
Actual Result:
The URL I have provided has “Wine” 1 time on the website, but it is performing the “Else” function by printing the message.

The Code:
import requests
from bs4 import BeautifulSoup
import time
import smtplib

while True:
# set the url,
url = “Profile: Dionysus

# set the headers like we are a browser,
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}

# download the URL
response = requests.get(url, headers=headers)

# parse the downloaded URL and grab all text
soup = BeautifulSoup(response.text, "lxml")

# if the number of times the defined word occurs on the page is 1,
if str(soup).find('Wine') == 1:
    # print "Please wait 15 seconds",
    print("Please wait 15 seconds")
    # pause 15 seconds,
    time.sleep(15)
    # continue with the script,
    continue
    
# but if the defined word occurs any other number of times,
else:
    print("Word occurs on website more or less than 1 time.")
    break

You can try .find function youself:

>>> str("wine xxxx").find("Wine")
-1
>>> str("Wine xxxx").find("Wine")
0
>>>

When includes you want, it will return 0.

1 Like

Thank you! I misunderstood this line of code. I get what it is doing now.
if str(soup).find(‘Wine’) == 1:

1 Like

In the general case, if str.find finds a match, it will return a non-negative integer (ie number 0 or higher), specifically the index where the first match is found. If you’re looking for substring existence, check out the in operator.

If you want to count the number of occurrences of “Wine” check out re.findall.

1 Like

That’s right. Thx :wink: