Skip to content

Commit

Permalink
scrolldown
Browse files Browse the repository at this point in the history
Signed-off-by: Kenneth Reitz <me@kennethreitz.org>
  • Loading branch information
kennethreitz committed Feb 27, 2018
1 parent c1646dd commit 7223ba4
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions requests_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,24 +224,41 @@ def __init__(self, *, url=DEFAULT_URL, html, default_encoding=DEFAULT_ENCODING)
def __repr__(self) -> str:
return "<HTML url={}>".format(repr(self.url))

def render(self, retries: int = 8, script: str = None):
def render(self, retries: int = 8, script: str = None, scrolldown = False, sleep: int = 0):
"""Reloads the response in Chromium, and replaces HTML content
with an updated version, with JavaScript executed.
If scrolldown is specified, the page will scrolldown the specified
number of times, after sleeping the specified amount of time.
Warning: the first time you run this method, it will download
Chromium into your home directory (``~/.pyppeteer``).
"""
async def _async_render(url: str, script: str = None):
async def _async_render(*, url: str, script: str = None, scrolldown, sleep: int):
try:
browser = pyppeteer.launch(headless=True)
page = await browser.newPage()

# Load the given page (GET request, obviously.)
await page.goto(url)

result = None
if script:
result = await page.evaluate(script)

if scrolldown:
for _ in range(scrolldown):
await page._keyboard.down('PageDown')
await asyncio.sleep(sleep)
# await page._keyboard.up('PageDown')


else:
await asyncio.sleep(sleep)

if scrolldown:
await page._keyboard.up('PageDown')

# Return the content of the page, JavaScript evaluated.
content = await page.content()
return content, result
Expand All @@ -254,7 +271,7 @@ async def _async_render(url: str, script: str = None):
for i in range(retries):
if not content:
try:
content, result = loop.run_until_complete(_async_render(url=self.url, script=script))
content, result = loop.run_until_complete(_async_render(url=self.url, script=script, sleep=sleep, scrolldown=scrolldown))
except TimeoutError:
pass

Expand Down

0 comments on commit 7223ba4

Please sign in to comment.