![]() ![]() The important decision here is that we want to be able to provide browser’s viewport dimensions. ![]() The code is very straightforward due to the pyppeteer’s simple API. The interesting thing is that pyppeteer has asynchronous API, so we will define our function also as asynchronous and later on use Python’s AsyncIO event loop to run it: async def capture_screenshot (url : str, path : Path, viewport_width : int, viewport_height : int ) - > None :Īwait page. ![]() The main code will start a headless Chromium, navigate to a page we want to capture and save its screenshot to a file of our choosing. Once this is done, we can start creating our program. Pyppeteer will also need a working copy of Chromium, which can be downloaded directly by invoking pyppeteer-install command which will become available after installation. So let’s begin by installing our dependencies: pip install pyppeteer You can learn more about Click in my other blog post Building command-line interfaces in Python. We can call our code in number of ways, but for this example I will use CLI library Click. Once the screenshot is taken, we will use Python imagining library Pillow to resize the resulting image to the size we want. It is a very useful tool in general and can be used to perform a number of tasks like web scraping, automation and making website screenshots! The advantage of using an actual browser to create website screenshots is clear: it gives us the very same result people will see in their own browsers. Pyppeteer is a Python port of a headless Chromium browser automation library puppeteer. Articles About me Creating website screenshots with Python and pyppeteer ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |