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

Unescapes html in PageParser.href_match_to_url #191

Merged
merged 1 commit into from
Jan 7, 2016
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
11 changes: 10 additions & 1 deletion pex/crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@
from urlparse import urlparse


def unescape(s):
"""Unescapes html. Taken from https://wiki.python.org/moin/EscapingHtml"""
s = s.replace("&lt;", "<")
s = s.replace("&gt;", ">")
# this has to be last:
s = s.replace("&amp;", "&")
return s


class PageParser(object):
"""A helper class to extract and differentiate ordinary and download links from webpages."""

Expand All @@ -34,7 +43,7 @@ class PageParser(object):
def href_match_to_url(cls, match):
def pick(group):
return '' if group is None else group
return pick(match.group(1)) or pick(match.group(2)) or pick(match.group(3))
return unescape(pick(match.group(1)) or pick(match.group(2)) or pick(match.group(3)))

@classmethod
def rel_links(cls, page):
Expand Down
6 changes: 6 additions & 0 deletions tests/test_crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ def test_page_parser_basic():
assert lpp("<a href='stuff'> <a href=%s>" % target) == (['stuff', href], [])


def test_page_parser_escaped_html():
url = 'url?param1=val&param2=val2'
link = 'a href="%s"' % url.replace('&', '&amp;')
assert lpp(link) == ([url], [])


def test_page_parser_rels():
VALID_RELS = tuple(PageParser.REL_TYPES)
for rel in VALID_RELS + ('', ' ', 'blah'):
Expand Down