mardi 24 septembre 2019

Coding monitor python

I am trying to implement a monitor for website. I would like to receive an email everytimes an item which is sold out became aviable. (Or if a new item is added)

When I run this code the output is: Process finished with exit code 0.

I'm a beginner with Python, is there something wrong? How do I run this code like 24/24? Do I receive the email? (Obviously there are my info in myaddress, password etc).

It looks like nothing happened ...

    import smtplib

    try:
      import urllib.request as urllib2
    except ImportError:
      import urllib2

    import hashlib
    import random
    import time


    # url to be scraped
    url = " # The URL I would like to monitor"

    # time between checks in seconds
    sleeptime = 60

    def getHash():
    # random integer to select user agent
    randomint = random.randint(0, 7)

    # User_Agents

user_agents = [
 # List of user agents
 # This will not prevent your IP from getting banned but will help a bit by pretending to be different browsers    
]

opener = urllib2.build_opener()
opener.addheaders = [('User-agent', user_agents[randomint])]
response = opener.open(url)
the_page = response.read()

return hashlib.sha224(the_page).hexdigest()


   current_hash = getHash()  # Get the current hash, which is what the website is now



   while 1:  # Run forever

      if getHash() == current_hash:  # If nothing has changed
      print
      "Not Changed"

   else:  # If something has changed
      email = 'myaddress@gmail.com' # Your email
      password = 'password' # Your email account password
      send_to_email = 'sentoaddreess@gmail.com' # Who you are sending the message to
      message = 'This is my message' # The message in the email

      server = smtplib.SMTP('smtp.gmail.com', 587) # Connect to the server
      server.starttls() # Use TLS
      server.login(email, password) # Login to the email server
      server.sendmail(email, send_to_email , message) # Send the email
      server.quit() # Logout of the email server

      break
      time.sleep(sleeptime)



Aucun commentaire:

Enregistrer un commentaire