mirror of
https://github.com/kennethreitz/requests-html.git
synced 2026-06-05 23:00:20 +00:00
Fix HTML class to use async iter and render on bare mode
This commit is contained in:
committed by
Alessandro Romano
parent
99f9c89766
commit
69bc5dcb20
+3
-2
@@ -410,7 +410,7 @@ class HTML(BaseParser):
|
||||
:param default_encoding: Which encoding to default to.
|
||||
"""
|
||||
|
||||
def __init__(self, *, session: Union['HTMLSession', 'AsyncHTMLSession'] = None, url: str = DEFAULT_URL, html: _HTML, default_encoding: str = DEFAULT_ENCODING) -> None:
|
||||
def __init__(self, *, session: Union['HTMLSession', 'AsyncHTMLSession'] = None, url: str = DEFAULT_URL, html: _HTML, default_encoding: str = DEFAULT_ENCODING, async_: bool = False) -> None:
|
||||
|
||||
# Convert incoming unicode HTML into bytes.
|
||||
if isinstance(html, str):
|
||||
@@ -423,7 +423,7 @@ class HTML(BaseParser):
|
||||
url=url,
|
||||
default_encoding=default_encoding
|
||||
)
|
||||
self.session = session or HTMLSession()
|
||||
self.session = session or async_ and AsyncHTMLSession() or HTMLSession()
|
||||
self.page = None
|
||||
self.next_symbol = DEFAULT_NEXT_SYMBOL
|
||||
|
||||
@@ -610,6 +610,7 @@ class HTML(BaseParser):
|
||||
|
||||
async def arender(self, retries: int = 8, script: str = None, wait: float = 0.2, scrolldown=False, sleep: int = 0, reload: bool = True, timeout: Union[float, int] = 8.0, keep_page: bool = False):
|
||||
""" Async version of render. Takes same parameters. """
|
||||
|
||||
self.browser = await self.session.browser
|
||||
content = None
|
||||
|
||||
|
||||
@@ -236,6 +236,29 @@ def test_bare_render():
|
||||
assert 'https://httpbin.org' in html.links
|
||||
|
||||
|
||||
@pytest.mark.render
|
||||
@pytest.mark.asyncio
|
||||
async def test_bare_arender():
|
||||
doc = """<a href='https://httpbin.org'>"""
|
||||
html = HTML(html=doc, async_=True)
|
||||
script = """
|
||||
() => {
|
||||
return {
|
||||
width: document.documentElement.clientWidth,
|
||||
height: document.documentElement.clientHeight,
|
||||
deviceScaleFactor: window.devicePixelRatio,
|
||||
}
|
||||
}
|
||||
"""
|
||||
val = await html.arender(script=script, reload=False)
|
||||
for value in ('width', 'height', 'deviceScaleFactor'):
|
||||
assert value in val
|
||||
|
||||
assert html.find('html')
|
||||
assert 'https://httpbin.org' in html.links
|
||||
await html.browser.close()
|
||||
|
||||
|
||||
@pytest.mark.render
|
||||
def test_bare_js_eval():
|
||||
doc = """
|
||||
@@ -257,6 +280,29 @@ def test_bare_js_eval():
|
||||
assert html.find('#replace', first=True).text == 'yolo'
|
||||
|
||||
|
||||
@pytest.mark.render
|
||||
@pytest.mark.asyncio
|
||||
async def test_bare_js_async_eval():
|
||||
doc = """
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<body>
|
||||
<div id="replace">This gets replaced</div>
|
||||
|
||||
<script type="text/javascript">
|
||||
document.getElementById("replace").innerHTML = "yolo";
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
|
||||
html = HTML(html=doc, async_=True)
|
||||
await html.arender()
|
||||
|
||||
assert html.find('#replace', first=True).text == 'yolo'
|
||||
await html.browser.close()
|
||||
|
||||
|
||||
@pytest.mark.ok
|
||||
def test_browser_session():
|
||||
""" Test browser instaces is created and properly close when session is closed.
|
||||
|
||||
Reference in New Issue
Block a user