In Requests-HTML, you can set the height and width dimensions for sending a request by utilizing the render
method. The render
method allows you to execute JavaScript within the context of the page, enabling you to modify its appearance before parsing and retrieving the content.
Here's an example of how to set the height and width dimensions when sending a request with Requests-HTML:
pythonfrom requests_html import HTMLSession
# Create an HTML session
session = HTMLSession()
# Define the URL you want to request
url = 'https://example.com'
# Define the desired height and width dimensions
desired_height = 800
desired_width = 1200
# Send a request and render the page with the specified dimensions
response = session.get(url, render=True, scrolldown=1, sleep=2, keep_page=True, browser_args=['--window-size={},{}'.format(desired_width, desired_height)])
# Access the rendered content
content = response.html.html
# Now you can parse and manipulate the content as needed
In the code above, we use the render
method with render=True
to enable rendering of the page, and we set the desired_height
and desired_width
variables to the desired dimensions. We pass the --window-size
argument to the browser instance via browser_args
to set the height and width dimensions.
Please note that using render=True
and rendering the page with specific dimensions may require additional dependencies like pyppeteer
or pyppdf
. Make sure you have installed these dependencies and the appropriate browser binaries to enable rendering.
Keep in mind that using rendering with Requests-HTML might add extra overhead and dependencies to your project, so use it judiciously based on your specific use case and performance requirements. If you don't need JavaScript rendering and just want to fetch and parse the HTML content, the basic get
method without render=True
should suffice.