Can you take a screenshot of a website without opening it in the browser using Python?

I can only download the html code but it is not possible to create a screenshot

(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
4 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
ralphdieter
5 months ago

To do this, you need a module that can convert HTML into an image, e.g.

> pip install html2image

According to the description, this is quite easy:

from html2image import Html2Image
hti = Html2Image()
hti.screenshot_url('https://www.python.org', 
                   'python_org.png', 
                   size=(800, 400))

If this does not happen under Android: There are zig other such modules. Just search for “python html to image” (or in German: “Transform pthon html into png”).

Mauritan
5 months ago

Yes and no.

It’s the question of what a “screenshot” is. A call, an ad?

If you display the page in html, you will also call it. If you want to know what it looks like, you need to turn the html into a graphical display. This either makes your browser or many other apps. If you have the html code, you don’t need to be online, then it’s offline.

Note that some things are connected to the server of the page. If the page is approximately interactive, this can be done, for example, a php script running on the server of the page.

Try this:

Save the html code as text, for example with Notepad or Word, no matter, Ending txt. Close the text editor.

Now you name the page in *.html

Right-click and “open with…” Now you choose any browser. It will show you how the html text file stored by you “views”.

LUCKY1ONE
5 months ago

With Selenium in headless mode.

AxolotlKing
4 months ago

This is because a website sends you only the HTML code once it is on your PC, which is then rendered by a browser engine. So you need some kind of browser to get a website as a picture. The easiest thing I think would be if you were using Selenium, this is a program that virtually controls your browser, and you can write the code for it in Python.