simonw / shot-scraper

A command-line utility for taking automated screenshots of websites
https://shot-scraper.datasette.io
Apache License 2.0
1.57k stars 70 forks source link

How to call shot-scraper from Python? #69

Closed robintw closed 2 years ago

robintw commented 2 years ago

I've just started using shot-scraper, and have been really impressed - thank you for creating it.

I want to programmatically set some of the command-line arguments, and rather than running it from Python using subprocess, I thought I'd try and run it directly from Python, as shot-scraper is written in Python itself.

I found the promising looking shot function in the cli module - but no matter how I call it, I get an error saying that the arguments aren't valid, or raising an AssertionError or similar. Is it not possible to manually call a function that has been decorated with click decorators like this? Is there any other way to call this from Python without re-implementing most of the shot function in my own code?

simonw commented 2 years ago

It's not really designed as a Python library at the moment, so if you import functions from it directly I can't guarantee your code won't break with future updates to shot-scraper.

You could try using the take_shot() function directly, but I instead recommend using the Click CLIRunner mechanism - something like this:

from click.testing import CliRunner
from shot_scraper import cli
runner = CliRunner()
result = runner.invoke(cli.cli, ["shot", "https://www.example.com/"])

Since this is itself calling the CLI interface defied for the tool, this won't break with future updates to shot-scraper (at least as long as those updates don't represent a breaking change to the CLI interface - and I plan not to make changes that break that).

wanghaisheng commented 2 years ago

there is nothing output

from click.testing import CliRunner
from shot_scraper import cli
runner = CliRunner()
result = runner.invoke(cli.cli, ["shot", "https://www.baidu.com/"])
from click.testing import CliRunner
from shot_scraper import cli
print('---')
runner = CliRunner()
result = runner.invoke(cli.cli, ["shot","www.baidu.com"])
result = runner.invoke(cli.cli, ["shot","www.baidu.com","--width 412", "--height 915"])
result = runner.invoke(cli.cli, ["shot","www.baidu.com --width 412  --height 915"])

result = runner.invoke(cli.cli, ["shot", "pdf","www.baidu.com"])
result = runner.invoke(cli.cli, ["shot", "pdf  www.baidu.com"])

only the first works @simonw