samedi 5 août 2017

Python Monitor Website for changes

I would like to login to a website, get the data, save it into a file, after some time get the new data and compare it with the old (saved) data and print if something has changed. How do I do that? The login is working, but the compare isn't. Why?

Thank you in advance!

My code:

# -*- coding: utf-8 -*-
import urllib
import urllib2
import cookielib
import time


def login():
    username = "username"
    password = "password"

    cj = cookielib.CookieJar()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    login_data = urllib.urlencode({'login_username' : username, 'login_password' : password})
    opener.open('lol.com/login', login_data)
    resp = opener.open('lol.com/login')
    data = resp.read()
    print data    
    write_data(data)

def write_data(data):
    file = open("htmlString", "w")
    file.write(data)
    file.close()
    monitor(data)



def monitor(data):
    string1 = open("htmlString", "r").read()
    string2 = data
    while True:
        time.sleep(5)
        login()
        if string1 == string2:
            print "Nothing has changed"
        else:
            print "Something has changed"




login()

Aucun commentaire:

Enregistrer un commentaire