Skip to content

Commit

Permalink
fix things
Browse files Browse the repository at this point in the history
Signed-off-by: Kenneth Reitz <[email protected]>
  • Loading branch information
kennethreitz committed Feb 26, 2018
1 parent 5a83a88 commit 00f5dcf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
9 changes: 6 additions & 3 deletions requests_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from parse import search as parse_search
from parse import findall


DEFAULT_ENCODING = 'utf-8'

useragent = UserAgent()

Expand Down Expand Up @@ -95,9 +95,9 @@ def gen():
else:
return c

def xpath(self, selector, first=False):
def xpath(self, selector, first=False, _encoding=None):
"""Given an XPath selector, returns a list of element objects."""
c = [Element(element=e, url=self.url, encoding=self.encoding) for e in self.lxml.xpath(selector)]
c = [Element(element=e, url=self.url, default_encoding=_encoding or self.encoding) for e in self.lxml.xpath(selector)]
if first:
try:
return c[0]
Expand Down Expand Up @@ -250,8 +250,11 @@ def _handle_response(response, **kwargs):
"""Requests HTTP Response handler. Attaches .html property to Response
objects.
"""
if not response.encoding:
response.encoding = DEFAULT_ENCODING

response.html = HTML(response=response)

return response


Expand Down
9 changes: 8 additions & 1 deletion tests/test_requests_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ def get():
path = os.path.sep.join((os.path.dirname(os.path.abspath(__file__)), 'python.html'))
url = 'file://{}'.format(path)

return session.get(url)
r = session.get(url)
r.encoding = 'utf-8'

return r


def test_file_get():
Expand All @@ -21,6 +24,7 @@ def test_file_get():

def test_css_selector():
r = get()

about = r.html.find('#about', first=True)

for menu_item in (
Expand Down Expand Up @@ -57,3 +61,6 @@ def test_xpath():
html = r.html.xpath('/html', first=True)
assert 'no-js' in html.attrs['class']

if __name__ == '__main__':
# test_file_get()
test_css_selector()

0 comments on commit 00f5dcf

Please sign in to comment.