screamshot package

generate_bytes_img

screamshot.generate_bytes_img_functions.generate_bytes_img(url: str, **kwargs) → bytes

This function takes a screenshot and returns it as a bytes object

Parameters
  • url (str) – mandatory, the website’s url

  • path (str) – optional, the path to the image output

  • credentials (dict) – If the website uses “login authentication”, you can specify two fields: username and password. Otherwise, if it uses a “token identification” that must be specified in the header, please indicate into credentials the field that should be passed to the header, as well as a token_in_header field equal to True.

  • width (int) – optionnal, the window’s width

  • height (int) – optionnal, the window’s height

  • selector (str) – optionnal, CSS3 selector, item whose screenshot is taken

  • wait_for (str) – optionnal, CSS3 selector, item to wait before taking the screenshot

  • wait_until (str or list(str)) – optionnal, define how long you wait for the page to be loaded should be either load, domcontentloaded, networkidle0 or networkidle2

Returns

the binary code of the image

Retype

bytes

Note

A ScreamshotException.BadUrl and a ScreamshotException.BadSelector can be raised.

Note

The asyncio library can be used to manipulate this function.

Warning

It uses pyppeteer and so async functions.

Exemple

import asyncio

from screamshot import generate_bytes_img


async def main():
    img = await generate_bytes_img('https://makina-corpus.com/expertise/cartographie',
                            selector='.image-right', wait_until='networkidle0')
    print(img)


if __name__ == '__main__':
    asyncio.get_event_loop().run_until_complete(main())

generate_bytes_img_prom

screamshot.generate_bytes_img_functions.generate_bytes_img_prom(url: str, future: _asyncio.Future, **kwargs)

This function takes a screenshot and returns it as a bytes object in the promise given in the parameters

Parameters
  • url (str) – mandatory, the website’s url

  • future (asyncio.Future) – mandatory, a promise

  • path (str) – optional, the path to the image output

  • credentials (dict) – If the website uses “login authentication”, you can specify two fields: username and password. Otherwise, if it uses a “token identification” that must be specified in the header, please indicate into credentials the field that should be passed to the header, as well as a token_in_header field equal to True.

  • width (int) – optionnal, the window’s width

  • height (int) – optionnal, the window’s height

  • selector (str) – optionnal, CSS3 selector, item whose screenshot is taken

  • wait_for (str) – optionnal, CSS3 selector, item to wait before taking the screenshot

  • wait_until (str or list(str)) – optionnal, define how long you wait for the page to be loaded should be either load, domcontentloaded, networkidle0 or networkidle2

Retype

None

Note

A ScreamshotException.BadUrl and a ScreamshotException.BadSelector can be raised.

Warning

It uses pyppeteer and so async functions.

Warning

This function must be used with the asyncio library.

Exemple

# views.py in a Django project
import asyncio

from django.http import HttpResponse

from screamshot import generate_bytes_img_prom


def home(request):
    loop = asyncio.get_event_loop()
    future = asyncio.Future()

    asyncio.ensure_future(
        generate_bytes_img_prom('https://www.google.fr', future))
    loop.run_until_complete(future)

    print(futur.result())
    return HttpResponse('Done')

generate_bytes_img_wrap

screamshot.generate_bytes_img_functions.generate_bytes_img_wrap(url: str, **kwargs)

This function takes a screenshot and returns it as a bytes object in synchorouns mode, usable directly in django

Parameters
  • url (str) – mandatory, the website’s url

  • path (str) – optional, the path to the image output

  • width (int) – optionnal, the window’s width

  • height (int) – optionnal, the window’s height

  • selector (str) – optionnal, CSS3 selector, item whose screenshot is taken

  • wait_for (str) – optionnal, CSS3 selector, item to wait before taking the screenshot

  • wait_until (str or list(str)) – optionnal, define how long you wait for the page to be loaded should be either load, domcontentloaded, networkidle0 or networkidle2

Returns

the binary code of the image

Retype

bytes

Note

A ScreamshotException.BadUrl and a ScreamshotException.BadSelector can be raised.

bytes_to_file

screamshot.bytes_to_file_function.bytes_to_file(b_img: bytes, path: str, img_format: str = None)

Transform the byte object representing an image into a file

Parameters
  • b_img (bytes) – the image

  • path (str) – the path to the future file

  • img_format (str) – the type of the image file, default value: type induced by path, or png if not found