Skip to content

Commit be5f7c2

Browse files
author
M.Michel
committed
add: send session.cookies to render
1 parent 09c8f34 commit be5f7c2

File tree

1 file changed

+65
-3
lines changed

1 file changed

+65
-3
lines changed

requests_html.py

+65-3
Original file line numberDiff line numberDiff line change
@@ -499,14 +499,18 @@ async def __anext__(self):
499499
def add_next_symbol(self, next_symbol):
500500
self.next_symbol.append(next_symbol)
501501

502-
async def _async_render(self, *, url: str, script: str = None, scrolldown, sleep: int, wait: float, reload, content: Optional[str], timeout: Union[float, int], keep_page: bool):
502+
async def _async_render(self, *, url: str, script: str = None, scrolldown, sleep: int, wait: float, reload, content: Optional[str], timeout: Union[float, int], keep_page: bool, cookies: list = [{}]):
503503
""" Handle page creation and js rendering. Internal use for render/arender methods. """
504504
try:
505505
page = await self.browser.newPage()
506506

507507
# Wait before rendering the page, to prevent timeouts.
508508
await asyncio.sleep(wait)
509509

510+
if cookies:
511+
for cookie in cookies:
512+
await page.setCookie(cookie)
513+
510514
# Load the given page (GET request, obviously.)
511515
if reload:
512516
await page.goto(url, options={'timeout': int(timeout * 1000)})
@@ -538,7 +542,60 @@ async def _async_render(self, *, url: str, script: str = None, scrolldown, sleep
538542
page = None
539543
return None
540544

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):
545+
def _convert_cookie_to_render(self, session_cookie):
546+
"""
547+
Convert HTMLSession.cookies[] for browser.newPage().setCookie
548+
"""
549+
# | setCookie(self, *cookies:dict) -> None
550+
# | Set cookies.
551+
# |
552+
# | ``cookies`` should be dictionaries which contain these fields:
553+
# |
554+
# | * ``name`` (str): **required**
555+
# | * ``value`` (str): **required**
556+
# | * ``url`` (str)
557+
# | * ``domain`` (str)
558+
# | * ``path`` (str)
559+
# | * ``expires`` (number): Unix time in seconds
560+
# | * ``httpOnly`` (bool)
561+
# | * ``secure`` (bool)
562+
# | * ``sameSite`` (str): ``'Strict'`` or ``'Lax'``
563+
cookie_render = {}
564+
def __convert(cookie, key):
565+
try:
566+
v = eval ("cookie."+key)
567+
if not v: kv = ''
568+
else: kv = {key: v}
569+
except:
570+
kv = ''
571+
return kv
572+
573+
keys = [
574+
'name',
575+
'value',
576+
'url',
577+
'domain',
578+
'path',
579+
'sameSite',
580+
'expires',
581+
'httpOnly',
582+
'secure',
583+
]
584+
for key in keys:
585+
cookie_render.update(__convert(session_cookie, key))
586+
return cookie_render
587+
588+
def _convert_cookies_to_render(self):
589+
"""
590+
Convert HTMLSession.cookies for browser.newPage().setCookie
591+
Return a list of dict
592+
"""
593+
cookies_render = []
594+
for cookie in self.session.cookies:
595+
cookies_render.append(self._convert_cookie_to_render(cookie))
596+
return cookies_render
597+
598+
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, cookies: list = [{}], send_cookies_session: bool = False):
542599
"""Reloads the response in Chromium, and replaces HTML content
543600
with an updated version, with JavaScript executed.
544601
@@ -550,6 +607,9 @@ def render(self, retries: int = 8, script: str = None, wait: float = 0.2, scroll
550607
:param reload: If ``False``, content will not be loaded from the browser, but will be provided from memory.
551608
:param keep_page: If ``True`` will allow you to interact with the browser page through ``r.html.page``.
552609
610+
:param send_cookies_session: If ``True`` send ``HTMLSession.cookies`` convert.
611+
:param cookies: If not ``empty`` send ``cookies``.
612+
553613
If ``scrolldown`` is specified, the page will scrolldown the specified
554614
number of times, after sleeping the specified amount of time
555615
(e.g. ``scrolldown=10, sleep=1``).
@@ -590,12 +650,14 @@ def render(self, retries: int = 8, script: str = None, wait: float = 0.2, scroll
590650
if self.url == DEFAULT_URL:
591651
reload = False
592652

653+
if send_cookies_session:
654+
cookies = self._convert_cookies_to_render()
593655

594656
for i in range(retries):
595657
if not content:
596658
try:
597659

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))
660+
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, cookies=cookies))
599661
except TypeError:
600662
pass
601663
else:

0 commit comments

Comments
 (0)