MCPcopy
hub / github.com/psf/requests-html / render

Method render

requests_html.py:541–610  ·  view source on GitHub ↗

Reloads the response in Chromium, and replaces HTML content with an updated version, with JavaScript executed. :param retries: The number of times to retry loading the page in Chromium. :param script: JavaScript to execute upon page load (optional). :param wait: The

(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)

Source from the content-addressed store, hash-verified

539 return None
540
541 def render(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):
542 """Reloads the response in Chromium, and replaces HTML content
543 with an updated version, with JavaScript executed.
544
545 :param retries: The number of times to retry loading the page in Chromium.
546 :param script: JavaScript to execute upon page load (optional).
547 :param wait: The number of seconds to wait before loading the page, preventing timeouts (optional).
548 :param scrolldown: Integer, if provided, of how many times to page down.
549 :param sleep: Integer, if provided, of how many long to sleep after initial render.
550 :param reload: If ``False``, content will not be loaded from the browser, but will be provided from memory.
551 :param keep_page: If ``True`` will allow you to interact with the browser page through ``r.html.page``.
552
553 If ``scrolldown`` is specified, the page will scrolldown the specified
554 number of times, after sleeping the specified amount of time
555 (e.g. ``scrolldown=10, sleep=1``).
556
557 If just ``sleep`` is provided, the rendering will wait *n* seconds, before
558 returning.
559
560 If ``script`` is specified, it will execute the provided JavaScript at
561 runtime. Example:
562
563 .. code-block:: python
564
565 script = \"\"\"
566 () => {
567 return {
568 width: document.documentElement.clientWidth,
569 height: document.documentElement.clientHeight,
570 deviceScaleFactor: window.devicePixelRatio,
571 }
572 }
573 \"\"\"
574
575 Returns the return value of the executed ``script``, if any is provided:
576
577 .. code-block:: python
578
579 >>> r.html.render(script=script)
580 {'width': 800, 'height': 600, 'deviceScaleFactor': 1}
581
582 Warning: the first time you run this method, it will download
583 Chromium into your home directory (``~/.pyppeteer``).
584 """
585
586 self.browser = self.session.browser # Automatically create a event loop and browser
587 content = None
588
589 # Automatically set Reload to False, if example URL is being used.
590 if self.url == DEFAULT_URL:
591 reload = False
592
593
594 for i in range(retries):
595 if not content:
596 try:
597
598 content, result, page = self.session.loop.run_until_complete(self._async_render(url=self.url, script=script, sleep=sleep, wait=wait, content=self.html, reload=reload, scrolldown=scrolldown, timeout=timeout, keep_page=keep_page))

Callers 4

test_bare_renderFunction · 0.95
test_bare_js_evalFunction · 0.95
test_renderFunction · 0.80
test_browser_processFunction · 0.80

Calls 3

_async_renderMethod · 0.95
MaxRetriesClass · 0.85
HTMLClass · 0.85

Tested by 4

test_bare_renderFunction · 0.76
test_bare_js_evalFunction · 0.76
test_renderFunction · 0.64
test_browser_processFunction · 0.64