9

I want use Python to diff two HTML files:

Example :

html_1 = """
<p>i love it</p>
"""
html_2 = """ 
<h2>i love it </p>
"""

the diff file will be like this :

diff_html = """
<del><p>i love it</p></dev><ins><h2>i love it</h2></ins>
"""

Is there a Python lib to help me do this?

0

6 Answers 6

13

lxml can do something similar to what you want. From the docs:

>>> from lxml.html.diff import htmldiff
>>> doc1 = '''<p>Here is some text.</p>'''
>>> doc2 = '''<p>Here is <b>a lot</b> of <i>text</i>.</p>'''
>>> print htmldiff(doc1, doc2)
<p>Here is <ins><b>a lot</b> of <i>text</i>.</ins> <del>some text.</del> </p>

I don't know of any other Python library for this specific task, but you may want to look into word-by-word diffs. They may approximate what you want.

One example is this one, implemented in both PHP and Python (save it as diff.py, then import diff)

>>> diff.htmlDiff(a,b)
>>> '<del><p>i</del> <ins><h2>i</ins> love <del>it</p></del> <ins>it </p></ins>'
Sign up to request clarification or add additional context in comments.

5 Comments

i have try it , but what i got is >>> from lxml.html.diff import htmldiff >>> doc1 = '''<p>Here is some text.</p>''' >>> doc2 = '''<h2>Here is some text.</h2>''' >>> print htmldiff(doc1, doc2) <h2>Here is some text.</h2>
my tag p have changed to chagne h2 , but it didn't show the difference
the lxml.html.diff docuemnt say: Markup is generally ignored, with the markup from new_html preserved, and possibly some markup from old_html (though it is considered acceptable to lose some of the old markup). Only the words in the HTML are diffed.
You're right, it works much worse than I remembered. I added an alternative, it's not exactly what you want but may be of help.
That's nice! If it works for you you should post it as an answer and accept it.
2

Checkout diff2HtmlCompare (full disclosure: I'm the author). If you're trying to just visualize the differences, then this may help you. If you are trying to extract the differences and do something with it, then you can use difflib as suggested by others (the script above just wraps difflib and uses pygments for syntax highlighting). Doug Hellmann has done a pretty good job detailing how to use difflib, I'd suggest checking out his tutorial.

Comments

2

I found two Python lib that's helpful:

  1. htmltreediff
  2. htmldiff

However both of it use Python's difflib lib to diff text, while I want to use google's diff.

Comments

1

You could use difflib.ndiff() to look for and replace the "-"/"+" with your desired HTML.

import difflib

html_1 = """
<p>i love it</p>
"""
html_2 = """
<h2>i love it </p>
"""

diff_html = ""
theDiffs = difflib.ndiff(html_1.splitlines(), html_2.splitlines())
for eachDiff in theDiffs:
    if (eachDiff[0] == "-"):
        diff_html += "<del>%s</del>" % eachDiff[1:].strip()
    elif (eachDiff[0] == "+"):
        diff_html += "<ins>%s</ins>" % eachDiff[1:].strip()

print diff_html

The result:

<del><p>i love it</p></del><ins><h2>i love it </p></ins>

Comments

0

AFAIK, python has a build in difflib that can do this.

Comments

0

Not exactly what your output is, but the standard library difflib has a simple htmldiff tool in it, which will build a html diff table for you.

import difflib

html_1 = """
<p>i love it</p>
"""
html_2 = """ 
<h2>i love it </p>
"""

htmldiff = difflib.HtmlDiff()
html_table = htmldiff.make_table([html_1], [html_2]) # each item is a list of lines

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.