How can I take a screenshot/image of a website using Python?
On the Mac, there's webkit2png and on Linux+KDE, you can use khtml2png. I've tried the former and it works quite well, and heard of the latter being put to use.
I recently came across QtWebKit which claims to be cross platform (Qt rolled WebKit into their library, I guess). But I've never tried it, so I can't tell you much more.
The QtWebKit links shows how to access from Python. You should be able to at least use subprocess to do the same with the others.
Take a screenshot of open website in python script
This solution is fairly cross-platform, but if you're trying to show a bit of a web page open in a desktop with menu/toolbars etc... it's not what you want.
You could use SeleniumHQ to automate a browser of your choice, have that browser render the page, then get the complete image of the rendered page - ie, not just a screenshot of the portion of the page that's displayed on screen. You could then crop that accordingly.
Take a screenshot from a website from commandline or with python
Sometimes you need extra http headers such User-Agent to get downloads to work. In python 2.7, you can:
import urllib2
request = urllib2.Request(
r'http://books.google.de/books?id=gikDAAAAMBAJ&pg=PA1&img=1&w=2500',
headers={'User-Agent':'Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 firefox/2.0.0.11'})
page = urllib2.urlopen(request)
with open('somefile.png','wb') as f:
f.write(page.read())
Or you can look at the params for adding http headers in wget or curl.
Is it possible to take a screenshot of a website using python without having to actually open the website on my pc?
You can use imgkit module to take screenshots without opening the site on your computer!
import imgkit
imgkit.from_url('http://google.com', 'out.jpg')
where you can also pass multiple URLs to take screenshots!
imgkit.from_url(['google.com', 'yandex.ru', 'engadget.com'], 'out.jpg')
There are more ways to make the website's screenshot. But, I prefer this, because we can take screenshots with just a line of code!
How to take screenshot of a given url using python
Did you try use Pyppeteer https://github.com/miyakogi/pyppeteer?
With fullPage
parameter you can take a screenshot of the entire page.
import asyncio
from pyppeteer import launch
async def main():
browser = await launch(headless=True)
page = await browser.newPage()
await page.goto('https://stackoverflow.com/questions/51000899/better-way-to-take-screenshot-of-a-url-in-python')
await page.screenshot({'path': 'screen.png', 'fullPage': True})
await browser.close()
asyncio.get_event_loop().run_until_complete(main())
EDIT
https://github.com/miyakogi/pyppeteer is not maintained.
New project: https://github.com/pyppeteer/pyppeteer
How to take screenshots in python?
To take a screenshot using python:
import pyscreenshot as ImageGrab
im = ImageGrab.grab()
im.save('path/to/image/folder/image_name.png')
im.show()
Take screenshot of full page with Selenium Python with chromedriver
How it works: set browser height as longest as you can...
#coding=utf-8
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
def test_fullpage_screenshot(self):
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--start-maximized')
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("yoururlxxx")
time.sleep(2)
#the element with longest height on page
ele=driver.find_element("xpath", '//div[@class="react-grid-layout layout"]')
total_height = ele.size["height"]+1000
driver.set_window_size(1920, total_height) #the trick
time.sleep(2)
driver.save_screenshot("screenshot1.png")
driver.quit()
if __name__ == "__main__":
test_fullpage_screenshot()
Related Topics
How to Get Href Links from HTML Using Python
Why Does Pyimport_Import Fail to Load a Module from the Current Directory
If _Name_ == '_Main_'' Equivalent in Ruby
Python Ctypes - Loading Dll Throws Oserror: [Winerror 193] %1 Is Not a Valid Win32 Application
How to Install Python Packages on Windows
Get List from Pandas Dataframe Column or Row
Merge Lists That Share Common Elements
How to Make a Single Legend for Many Subplots with Matplotlib
Selenium Give File Name When Downloading
Compare Two Columns Using Pandas
What's the Difference Between Dist-Packages and Site-Packages
How to Add Multiple Columns to Pandas Dataframe in One Assignment
Find Column Whose Name Contains a Specific String
Add Text to Existing PDF Using Python
Does SQLalchemy Have an Equivalent of Django's Get_Or_Create
Why Can Tuples Contain Mutable Items