I am currently working on my first python script, which is supposed to check a URL every XX seconds and notify me if the text on the url changed.
My problem is that I can't find a way to refer to a variable outside the function it was defined in.
I tried to use global variable, but this resulted in errors as well.
The current version refers to the variable soup within the scrape function (scrape.soup = doesn't return errors, while `soup = does).
However in line 15 it still has issues to find the variable soup as it gives me this notification:
Cannot find reference 'soup' in 'function'
from bs4 import BeautifulSoup
import requests
import time
sleeptime = 15
def scrape():
url = "http://www.pythonforbeginners.com"
source_code = requests.get(url)
plain_text = source_code.text
scrape.soup = BeautifulSoup(plain_text, 'html.parser')
while 1:
if scrape() == scrape.soup:
print('Nothing Changed')
else:
print("Something Changed!")
break
time.sleep(sleeptime)
I expect the script to save the html_text of 'url' in the variable 'soup'.
The script should compare the latest scrape with the old scrape and print notifications for each result.
In case nothing changed, it should print "nothing changed".
In case it changed, it should print "Something Changed".
The script is being without any errors. However, when running the script, it always returns "Something changed".
I am pretty sure this is not correct, as it wouldn't make sense that the content on the site changed every 15 seconds. In addition I feel there is an error with time.seep, as the script runs only once and doesn't repeat every 15 seconds
I would really appreciate any clues that would point me into the right direction.
scrape()doesn't return anything