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