From 75b20d7575551a4295e75e1c3ea34530e497d0a1 Mon Sep 17 00:00:00 2001 From: isudox Date: Thu, 1 Mar 2018 21:02:19 +0800 Subject: [PATCH 1/2] Add typing hints, and remove unnecessary code for constructor. --- requests_html.py | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/requests_html.py b/requests_html.py index 82a72c2..c8ca374 100644 --- a/requests_html.py +++ b/requests_html.py @@ -24,6 +24,7 @@ # Typing. _Find = Union[List['Element'], 'Element'] _XPath = Union[List[str], List['Element'], str, 'Element'] +_Result = Union[List['Result'], 'Result'] _HTML = Union[str, bytes] _BaseHTML = str _UserAgent = str @@ -44,6 +45,7 @@ except AssertionError: raise RuntimeError('Requests-HTML requires Python 3.6+!') + class BaseParser: """A basic HTML/Element Parser, for Humans. @@ -102,7 +104,8 @@ def encoding(self) -> _Encoding: return self._encoding if self._encoding else self.default_encoding @encoding.setter - def encoding(self, enc): + def encoding(self, enc: str) -> None: + """Property setter for self.encoding.""" self._encoding = enc @property @@ -134,7 +137,8 @@ def full_text(self) -> _Text: return self.lxml.text_content() def find(self, selector: str, first: bool = False, _encoding: str = None) -> _Find: - """Given a CSS Selector, returns a list of :class:`Element ` objects. + """Given a CSS Selector, returns a list of + :class:`Element ` objects or a single one. :param selector: CSS Selector to use. :param first: Whether or not to return just the first result. @@ -147,9 +151,13 @@ def find(self, selector: str, first: bool = False, _encoding: str = None) -> _Fi - ``a#someID`` - ``a[target=_blank]`` - See W3School's `CSS Selectors Reference `_ for more details. + See W3School's `CSS Selectors Reference + `_ + for more details. - If ``first`` is ``True``, only returns the first :class:`Element ` found.""" + If ``first`` is ``True``, only returns the first + :class:`Element ` found. + """ encoding = _encoding or self.encoding elements = [ @@ -161,7 +169,7 @@ def find(self, selector: str, first: bool = False, _encoding: str = None) -> _Fi def xpath(self, selector: str, first: bool = False, _encoding: str = None) -> _XPath: """Given an XPath selector, returns a list of - :class:`Element ` objects. + :class:`Element ` objects or a single one. :param selector: XPath Selector to use. :param first: Whether or not to return just the first result. @@ -188,15 +196,15 @@ def xpath(self, selector: str, first: bool = False, _encoding: str = None) -> _X return _get_first_or_list(elements, first) def search(self, template: str) -> Result: - """Searches the :class:`Element ` for the given Parse template. + """Search the :class:`Element ` for the given Parse template. :param template: The Parse template to use. """ return parse_search(template, self.html) - def search_all(self, template: str) -> Result: - """Searches the :class:`Element ` (multiple times) for the given parse + def search_all(self, template: str) -> _Result: + """Search the :class:`Element ` (multiple times) for the given parse template. :param template: The Parse template to use. @@ -269,7 +277,7 @@ class Element(BaseParser): :param default_encoding: Which encoding to default to. """ - def __init__(self, *, element, url, default_encoding) -> None: + def __init__(self, *, element, url: _URL, default_encoding: _DefaultEncoding = None) -> None: super(Element, self).__init__(element=element, url=url, default_encoding=default_encoding) self.element = element @@ -299,11 +307,7 @@ class HTML(BaseParser): :param default_encoding: Which encoding to default to. """ - def __init__(self, *, url: str = DEFAULT_URL, html: _HTML, default_encoding: str =DEFAULT_ENCODING) -> None: - - # Convert incoming unicode HTML into bytes. - if isinstance(html, str): - html = html.encode(DEFAULT_ENCODING) + def __init__(self, *, url: str = DEFAULT_URL, html: _HTML, default_encoding: str = DEFAULT_ENCODING) -> None: super(HTML, self).__init__( # Convert unicode HTML to bytes. @@ -438,7 +442,7 @@ def user_agent(style='chrome') -> _UserAgent: return useragent[style] if style else useragent.random -def _get_first_or_list(l, first=True): +def _get_first_or_list(l, first=False): if first: try: return l[0] @@ -453,8 +457,8 @@ class HTMLSession(requests.Session): amongst other things. """ - def __init__(self, mock_browser=True, *args, **kwargs): - super(HTMLSession, self).__init__(*args, **kwargs) + def __init__(self, mock_browser=True): + super(HTMLSession, self).__init__() # Mock a web browser's user agent. if mock_browser: From 8fab96cdffec5a40027c3280aea160161e9bacef Mon Sep 17 00:00:00 2001 From: isudox Date: Thu, 1 Mar 2018 22:05:47 +0800 Subject: [PATCH 2/2] Reset my careless changes. --- requests_html.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/requests_html.py b/requests_html.py index 0a049d9..e49c3c7 100644 --- a/requests_html.py +++ b/requests_html.py @@ -309,6 +309,10 @@ class HTML(BaseParser): def __init__(self, *, url: str = DEFAULT_URL, html: _HTML, default_encoding: str = DEFAULT_ENCODING) -> None: + # Convert incoming unicode HTML into bytes. + if isinstance(html, str): + html = html.encode(DEFAULT_ENCODING) + super(HTML, self).__init__( # Convert unicode HTML to bytes. element=PyQuery(html)('html') or PyQuery('{}'.format(html))('html'),