Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
765 views
in Technique[技术] by (71.8m points)

browser - Take a screenshot of open website in python script

I need to write a python script which opens a website and when the website is completly opened it takes a screenshot of the opened website.

I wrote sth like this:

import webbrowser
import wx

wx.App()
link = "http://stackoverflow.com/questions"
webbrowser.get('firefox %s').open_new_tab(link)
screen = wx.ScreenDC()
size = screen.GetSize()
bmp = wx.EmptyBitmap(size[0], size[1])
mem = wx.MemoryDC(bmp)
mem.Blit(0, 0, size[0], size[1], screen, 0, 0)
del mem
bmp.SaveFile('screenshot.png', wx.BITMAP_TYPE_PNG)

It only opens a new tab in firefox but it doesnt take a screenshot of it :(

I want the solution to be cross-platform. Thanks for any help:)

EDIT:

The main problem here is that the script musnt take a picture BEFORE my webpage is completly opened. How to solve that issue?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I believe that you need different solutions for different operating systems. Use sys.platform to find out on which platform you are on. The rest you have to figure out yourself, but a quick internet search revealed:

  • On Linux systems, you can then take a screenshot as described here:

Take a screenshot via a python script. [Linux]

  • On Windows systems, you can base your solution on this answer:

Fastest way to take a screenshot with python on windows

  • On Mac systems, this will help:

Take screenshot in Python on Mac OS X


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...