Tools zum Erfassen und Konvertieren des Webs

Erfassen Sie Website-Screenshots oder konvertieren Sie HTML in Bilder

Python-API

Erstellen Sie perfekte Screenshots von Websites oder konvertieren Sie HTML direkt in Bilder, indem Sie die folgenden Funktionen von verwenden GrabzIt's Python API. Bevor Sie jedoch anfangen, denken Sie daran, dass nach dem Aufruf der URLToImage, HTMLToImage or FileToImage Methoden der Save or SaveTo Methode muss aufgerufen werden, um den Screenshot zu machen.

Grundlegende Optionen

Es ist nur ein Parameter erforderlich, um einen Screenshot einer Webseite oder zu erstellen HTML konvertieren into ein Bild wie im folgenden Beispiel gezeigt.

grabzIt.URLToImage("https://www.tesla.com")
# Then call the Save or SaveTo method
grabzIt.HTMLToImage("<html><body><h1>Hello World!</h1></body></html>")
# Then call the Save or SaveTo method
grabzIt.FileToImage("example.html")
# Then call the Save or SaveTo method

Bildformate

Die Python-API von GrabzIt kann Screenshots in verschiedenen Formaten aufnehmen, darunter JPG, PNG, WEBP, BMP (8-Bit, 16-Bit, 24-Bit oder 32-Bit) und TIFF. Das Standardformat für Bild-Screenshots ist JPG. Unter diesen Umständen ist die Qualität eines JPG-Bilds für einige Anwendungen möglicherweise nicht gut genug. Für Screenshots wird das PNG-Format empfohlen, da es ein ausgewogenes Verhältnis zwischen Qualität und Dateigröße bietet. Das folgende Beispiel zeigt einen Screenshot, der im PNG-Format aufgenommen wurde.

from GrabzIt import GrabzItImageOptions
from GrabzIt import GrabzItClient

grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzItImageOptions.GrabzItImageOptions()
options.format = "png"

grabzIt.URLToImage("https://www.tesla.com", options)
# Then call the Save or SaveTo method
grabzIt.SaveTo("result.png")
from GrabzIt import GrabzItImageOptions
from GrabzIt import GrabzItClient

grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzItImageOptions.GrabzItImageOptions()
options.format = "png"

grabzIt.HTMLToImage("<html><body><h1>Hello World!</h1></body></html>", options)
# Then call the Save or SaveTo method
grabzIt.SaveTo("result.png")
from GrabzIt import GrabzItImageOptions
from GrabzIt import GrabzItClient

grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzItImageOptions.GrabzItImageOptions()
options.format = "png"

grabzIt.FileToImage("example.html", options)
# Then call the Save or SaveTo method
grabzIt.SaveTo("result.png")

Browsergröße

Die Browsergröße bezieht sich auf die Größe des Browserfensters, das beim Aufnehmen des Screenshots verwendet wird. In den meisten Fällen muss dies nicht festgelegt werden, da die Standard-Browsergröße für alle Aufgaben ausreicht. Um die Standardgröße des Browsers zu verwenden, übergeben Sie einfach 0 zu den browserWidth und browserHeight Attribute der GrabzItImageOptions Klasse.

Bildgröße ändern

Das Ändern der Bildgröße ist einfach, ohne dass das Bild verzerrt wird, ist jedoch etwas schwieriger. Um den gesamten Vorgang zu vereinfachen, empfehlen wir, dies zu verwenden einfacher Bildmaßrechner.

Wenn Sie die Breite und Höhe des Bilds auf eine Größe erhöhen möchten, die größer als die Breite und Höhe des Browsers ist (standardmäßig 1366 x 728 Pixel), müssen auch die Breite und Höhe des Browsers entsprechend erhöht werden.

Benutzerdefinierte Kennung

Sie können der einen benutzerdefinierten Bezeichner übergeben Image Bei den folgenden Methoden wird dieser Wert an Ihren GrabzIt Python-Handler zurückgegeben. Diese benutzerdefinierte Kennung kann beispielsweise eine Datenbankkennung sein, mit der ein Screenshot einem bestimmten Datenbankeintrag zugeordnet werden kann.

from GrabzIt import GrabzItImageOptions
from GrabzIt import GrabzItClient

grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzItImageOptions.GrabzItImageOptions()
options.customId = "123456"

grabzIt.URLToImage("https://www.tesla.com", options)
# Then call the Save method
grabzIt.Save("http://www.example.com/handler.py")
from GrabzIt import GrabzItImageOptions
from GrabzIt import GrabzItClient

grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzItImageOptions.GrabzItImageOptions()
options.customId = "123456"

grabzIt.HTMLToImage("<html><body><h1>Hello World!</h1></body></html>", options)
# Then call the Save method
grabzIt.Save("http://www.example.com/handler.py")
from GrabzIt import GrabzItImageOptions
from GrabzIt import GrabzItClient

grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzItImageOptions.GrabzItImageOptions()
options.customId = "123456"

grabzIt.FileToImage("example.html", options)
# Then call the Save method
grabzIt.Save("http://www.example.com/handler.py")

Screenshot in voller Länge

GrabzIt ermöglicht es Ihnen, einen Screenshot in voller Länge einer gesamten Webseite zu machen, um dies zu tun, müssen Sie ein -1 an das übergeben browserHeight Attribut. Um sicherzustellen, dass das Bild der Größe des Browsers entspricht, übergeben Sie ein -1 an das height und width Attribute.

from GrabzIt import GrabzItImageOptions
from GrabzIt import GrabzItClient

grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzItImageOptions.GrabzItImageOptions()
options.browserHeight = -1
options.width = -1
options.height = -1

grabzIt.URLToImage("https://www.tesla.com", options)
# Then call the Save or SaveTo method
grabzIt.SaveTo("result.jpg")
from GrabzIt import GrabzItImageOptions
from GrabzIt import GrabzItClient

grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzItImageOptions.GrabzItImageOptions()
options.browserHeight = -1
options.width = -1
options.height = -1

grabzIt.HTMLToImage("<html><body><h1>Hello World!</h1></body></html>", options)
# Then call the Save or SaveTo method
grabzIt.SaveTo("result.jpg")
from GrabzIt import GrabzItImageOptions
from GrabzIt import GrabzItClient

grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzItImageOptions.GrabzItImageOptions()
options.browserHeight = -1
options.width = -1
options.height = -1

grabzIt.FileToImage("example.html", options)
# Then call the Save or SaveTo method
grabzIt.SaveTo("result.jpg")

Sie können auch Miniaturansichten zurückgeben, die nicht zugeschnitten sind. Beachten Sie jedoch, dass dies zu großen Bildern führen kann. Übergeben Sie dazu ein -1 an das height und / oder width Attribute. Die Dimension, die als -1 übergeben wird, wird nicht zugeschnitten.

from GrabzIt import GrabzItImageOptions
from GrabzIt import GrabzItClient

grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzItImageOptions.GrabzItImageOptions()
options.width = -1
options.height = -1

grabzIt.URLToImage("https://www.tesla.com", options)
# Then call the Save or SaveTo method
grabzIt.SaveTo("result.jpg")
from GrabzIt import GrabzItImageOptions
from GrabzIt import GrabzItClient

grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzItImageOptions.GrabzItImageOptions()
options.width = -1
options.height = -1

grabzIt.HTMLToImage("<html><body><h1>Hello World!</h1></body></html>", options)
# Then call the Save or SaveTo method
grabzIt.SaveTo("result.jpg")
from GrabzIt import GrabzItImageOptions
from GrabzIt import GrabzItClient

grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzItImageOptions.GrabzItImageOptions()
options.width = -1
options.height = -1

grabzIt.FileToImage("example.html", options)
# Then call the Save or SaveTo method
grabzIt.SaveTo("result.jpg")
Beachten Sie, dass es keine Browser-Breite in voller Länge gibt!

Wenn Sie diese speziellen Werte verwenden, können Sie einen Screenshot erstellen, der eine Vollversion der gesamten Webseite ist, wenn Sie dies wünschen!

Machen Sie einen Screenshot eines Seitenelements

Mit GrabzIt können Sie einen Screenshot eines HTML-Elements wie z div or span Tag und erfassen Sie den gesamten Inhalt. Dazu muss das HTML-Element, das Sie scannen möchten, als a angegeben werden CSS-Selektor.

...
<div id="features">
	<img src="http://www.example.com/race.jpg"/><h3>Car Race Tommorow</h3>
</div>
...

Für das folgende Beispiel wählen wir das div mit der ID "features" aus und geben es als 250 x 250px JPEG-Bild aus.

from GrabzIt import GrabzItImageOptions
from GrabzIt import GrabzItClient

grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret")

# The 250 parameters indicates that image should be sized to 250 x 250 px
options = GrabzItImageOptions.GrabzItImageOptions()
options.width = 250
options.height = 250
options.format = "jpg"
options.targetElement = "#features"

grabzIt.URLToImage("http://www.bbc.co.uk/news", options)
# Then call the Save or SaveTo method
grabzIt.SaveTo("result.jpg")

Das nächste Beispiel zeigt einen weiteren Screenshot des Divs "features". Dieses Mal wird jedoch ein JPEG-Bild ausgegeben, das genau die Größe des Divs hat.

from GrabzIt import GrabzItImageOptions
from GrabzIt import GrabzItClient

grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret")

# The -1 indicates that image should not be cropped
options = GrabzItImageOptions.GrabzItImageOptions()
options.width = 250
options.height = 250
options.browserHeight = -1
options.format = "jpg"
options.targetElement = "#features"

grabzIt.URLToImage("http://www.bbc.co.uk/news", options)
# Then call the Save or SaveTo method
grabzIt.SaveTo("result.jpg")