Search code examples
pythoncommand-linewebscreenshot

Take a screenshot from a website from commandline or with python


i will take a screenshot from this page: http://books.google.de/books?id=gikDAAAAMBAJ&pg=PA1&img=1&w=2500 or save the image that it outputs.

But i can't find a way. With wget/curl i get an "unavailable error" and also with others tools like webkit2png/wkhtmltoimage/wkhtmltopng.

Is there a clean way to do it with python or from commandline?

Best regards!


Solution

  • 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.