Python Forum

Full Version: How to highlight html using python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi,

i have written a code that look for changes in website and produce it's html. but my concern is how can i look highlight the change that has happen.

below is my code:
from urllib.request import urlopen
import EmailAlert


class Changes:
    def __init__(self, url, archive='archived_page.html'):
        self.url = url
        self.new_html = []
        self.old_html = []
        self.change_list = []
        self.archive = archive

    def update(self):
        with urlopen(self.url) as byt:
            self.new_html = [x.decode('latin-1') for x in byt.readlines()]

        open(self.archive, 'a').close()

        with open(self.archive, 'rb+') as old:
            self.old_html = [x.decode('latin-1') for x in old.readlines()]
            old.seek(0)
            old.truncate()
            data = old.writelines(x.encode('latin-1') for x in self.new_html)        

    def diff(self):
        self.change_list = []
        for a, b in zip(self.old_html, self.new_html):
            if a != b:
                if not b.strip():
                    self.change_list.append(f'\nLine removed:\n{a}\n')
                elif not a.strip():
                    self.change_list.append(f'\nLine added:\n{b}\n')
                else:
                    self.change_list.append(f'\n{a}\nChanged to:\n{b}\n')
                    print(a, b)
        return self.change_list

    def email(self):
        message = f'{len(self.change_list)} changes in {self.url}:\n'
        message += ''.join(self.change_list)
        EmailAlert.send(message)


if __name__ == '__main__':
    link = 'file:///C:/Users/prince.bhatia/Desktop/projects/urlwatch-master/website-watcher-master/website-watcher-master/test/Maharashtra.htm'
    #link = "http://www.rera.mp.gov.in/"
    wiki_updater = Changes(link)
    wiki_updater.update()
    if wiki_updater.diff():
        print("Website Updated")
        wiki_updater.email()
Are you asking how to highlight something in HTML, how to detect the part to highlight, or both? Also you might want to clarify what you mean by "highlight" because it could be something as simple as adding a span tag with CSS that changes the background color to yellow, or it could be other things.
i am asking for both..to highlight the detected text in html.