Skip to content

Capture screenshot and/or HTML source code data of any given webpage and print it to stdout.

License

Notifications You must be signed in to change notification settings

elliotwutingfeng/take_a_shot

Repository files navigation

Take A Shot

Lens

Capture screenshot and/or HTML source code data of any given webpage and print it to stdout.

Output format: JSON string containing b64-encoded data.

Python Docker Google Chrome

MIT license

Implementation details

  • Uses Google Chrome in headless mode.
  • Works well even with vertically long webpages.
  • Webpages with fixed headers generally work well, but fixed footers may lead to unusual results.

Forked and modified from: https://github.com/mokemokechicken/docker_capture_web

Requirements

Tested on the following environment

Linux

  • Linux 6.6
  • Docker Version 24.0.7

Usage

Build

./build

Then either run

./capture <URL> [options]

or alternatively run

docker run --cap-drop=all --security-opt=no-new-privileges --rm elliotwutingfeng/take_a_shot <URL> [options]

Output Format

{
  "screenshot": "<PNG screenshot as b64-encoded string>",
  "source_code": "<HTML source code as b64-encoded string>"
}

Examples

Default User Agent

docker run --cap-drop=all --security-opt=no-new-privileges --rm elliotwutingfeng/take_a_shot "https://example.com"

Specify User Agent

docker run --cap-drop=all --security-opt=no-new-privileges --rm elliotwutingfeng/take_a_shot "https://example.com" --window-size 390x844 --ua 'Mozilla/5.0 (iPhone; CPU iPhone OS 16_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) FxiOS/113.0 Mobile/15E148 Safari/605.1.15'

Help

usage: takeashot.py [-h] [--screenshot] [--source-code]
                    [--window-size WINDOW_SIZE] [--ua USER_AGENT]
                    [--wait WAIT] [--lang LANG] [--language LANGUAGE] [-v]
                    [--vv]
                    url

positional arguments:
  url                   specify URL

options:
  -h, --help            show this help message and exit
  --screenshot          Take screenshot
  --source-code         Extract source code
  --window-size WINDOW_SIZE
                        specify window size like 1200x800
  --ua USER_AGENT       specify user-agent
  --wait WAIT           specify wait seconds after scroll down
  --lang LANG           set LANG environment variable
  --language LANGUAGE   set LANGUAGE environment variable
  -v                    set LogLevel to INFO
  --vv                  set LogLevel to DEBUG

Credits

Link