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
#2
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.
Reply
#3
i am asking for both..to highlight the detected text in html.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  highlight the columns and capture amy83 0 811 Mar-24-2023, 07:19 PM
Last Post: amy83
  Tkinterweb (Browser Module) Appending/Adding Additional HTML to a HTML Table Row AaronCatolico1 0 939 Dec-25-2022, 06:28 PM
Last Post: AaronCatolico1
  reading html and edit chekcbox to html jacklee26 5 3,096 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,302 Feb-24-2020, 06:19 AM
Last Post: shubhamjainj
  Highlight and remove specific string of text itsalmade 5 3,564 Dec-11-2019, 11:58 PM
Last Post: micseydel
  How to highlight the cell SriRajesh 3 3,014 Dec-08-2019, 02:17 PM
Last Post: michael1789
  [ library recommendation ] search & highlight target texts in PDF? smallabc 1 2,234 Nov-27-2019, 10:40 AM
Last Post: Larz60+
  HTML to Python to Windows .bat and back to HTML perfectservice33 0 1,956 Aug-22-2019, 06:31 AM
Last Post: perfectservice33
  Highlight text in pdf pramodb35 1 2,918 Aug-03-2019, 02:58 AM
Last Post: Larz60+
  Highlight/Underline a string | ValueError: zero length field name in format searching1 1 3,035 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