Python Forum
How to highlight html using python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to highlight html using python
#1
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()
Reply


Messages In This Thread
How to highlight html using python - by Prince_Bhatia - Apr-30-2019, 07:08 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  highlight the columns and capture amy83 0 827 Mar-24-2023, 07:19 PM
Last Post: amy83
  Tkinterweb (Browser Module) Appending/Adding Additional HTML to a HTML Table Row AaronCatolico1 0 967 Dec-25-2022, 06:28 PM
Last Post: AaronCatolico1
  reading html and edit chekcbox to html jacklee26 5 3,131 Jul-01-2021, 10:31 AM
Last Post: snippsat
  How to compare two columns and highlight the unique values of column two using pandas shubhamjainj 0 4,318 Feb-24-2020, 06:19 AM
Last Post: shubhamjainj
  Highlight and remove specific string of text itsalmade 5 3,614 Dec-11-2019, 11:58 PM
Last Post: micseydel
  How to highlight the cell SriRajesh 3 3,046 Dec-08-2019, 02:17 PM
Last Post: michael1789
  [ library recommendation ] search & highlight target texts in PDF? smallabc 1 2,256 Nov-27-2019, 10:40 AM
Last Post: Larz60+
  HTML to Python to Windows .bat and back to HTML perfectservice33 0 1,976 Aug-22-2019, 06:31 AM
Last Post: perfectservice33
  Highlight text in pdf pramodb35 1 2,940 Aug-03-2019, 02:58 AM
Last Post: Larz60+
  Highlight/Underline a string | ValueError: zero length field name in format searching1 1 3,051 Jul-01-2019, 03:06 AM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020