Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support load html from bytes #107

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
build/
dist/
.cache
.venv/
6 changes: 3 additions & 3 deletions html_similarity/structural_similarity.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import difflib
from io import StringIO
from io import BytesIO, StringIO

import lxml.html

Expand Down Expand Up @@ -32,8 +32,8 @@ def structural_similarity(document_1, document_2):
:return: int
"""
try:
document_1 = lxml.html.parse(StringIO(document_1))
document_2 = lxml.html.parse(StringIO(document_2))
document_1 = lxml.html.parse(StringIO(document_1) if isinstance(document_1, str) else BytesIO(document_1))
document_2 = lxml.html.parse(StringIO(document_2) if isinstance(document_2, str) else BytesIO(document_2))
except Exception as e:
print(e)
return 0
Expand Down
35 changes: 34 additions & 1 deletion tests/test_similarity.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from html_similarity import style_similarity
from html_similarity.style_similarity import jaccard_similarity
from html_similarity import structural_similarity

from .utils import almost_equal


html1 = ''''
<html>
<body class="body-class">
Expand Down Expand Up @@ -44,3 +44,36 @@ def test_jaccard_similarity():
assert almost_equal(0.6666, jaccard_similarity(['a', 'b'], ['a', 'b', 'c']))
assert 0 == jaccard_similarity(['d', 'e'], ['a', 'b', 'c'])
assert almost_equal(jaccard_similarity(list(range(1, 1000000)), list(range(1000000 - 10, 2 * 1000000))), 0)

xhtml_1 = '''
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<body class="body-class">
<h1 class="title">This a title<h1>
<ul>
<li class="item active">item 1</li>
<li class="item">item 2</li>
<li class="item">item 3</li>
<ul>
</body>
</html>
'''

xhtml_2 = '''
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<body class="body-class">
<h1 class="title">This a different title<h1>
<ul>
<li class="item active">item 1</li>
<li class="item">item 2</li>
<li class="item">item 3</li>
<ul>
</body>
</html>
'''

def test_bytes_similarity():
assert 1 == structural_similarity(xhtml_1.encode('utf-8'), xhtml_2.encode('utf-8'))