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

Fixes #60, attempts to add HTML tag if non exists #61

Merged
merged 1 commit into from
Feb 3, 2017
Merged
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
6 changes: 6 additions & 0 deletions lassie/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

"""


import json
import re
from os.path import basename

import requests
Expand Down Expand Up @@ -157,9 +159,13 @@ def fetch(self, url, open_graph=None, twitter_card=None, touch_icon=None,
})
else:
html, status_code = self._retrieve_content(url)

if not html:
raise LassieError('There was no content to parse.')

if not '<html' in html:
html = re.sub(r'(?:<!DOCTYPE(?:\s\w)?>(?:<head>)?)', '<!DOCTYPE html><html>', html)

soup = BeautifulSoup(clean_text(html), parser)

self._filter_amp_data(soup, data, url, all_images)
Expand Down
12 changes: 12 additions & 0 deletions tests/templates/core/no_html_tag.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>Lassie Generic Test | No HTML Tag</title>

<meta name="description" content="Just a random description of a web page." />
<meta name="keywords" content="one, two, three, four, five" />
<meta name="title" content="Lassie Generic Test | no_html_tag" />
</head>
<body>
</body>
</html>
8 changes: 8 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,11 @@ def test_prepare_request(self):

l = Lassie()
l._prepare_request('HEAD', url=url, headers=l.client.headers)

def test_no_html_tag(self):
url = 'http://lassie.it/core/no_html_tag.html'

l = Lassie()
data = l.fetch(url)

self.assertTrue('no_html_tag' in data['title'])