0

How to take a screenshot of a website with Python, Windows environment ?


Remarks :

  • The question of taking a screenshot of a website with Python has been highly discussed here but most of the solutions only work for Mac like webkit2png (see the discussion here about portability and also this answer) or Linux

  • The only half-working solution I've found is :

    from selenium import webdriver
    browser = webdriver.Firefox()
    browser.get('http://www.example.com')
    browser.save_screenshot('test.png')
    

    it works 50% of the time, but when doing it for 100 pages, in a loop, it always stops / is stuck / after 10 or 15 or 25 pages. Even if I time.sleep(...), etc.

Community
  • 1
  • 1
Basj
  • 41,386
  • 99
  • 383
  • 673
  • 1
    Are you getting any errors when it stops/crashes? – alecxe Feb 24 '16 at 14:24
  • No error, it's just stuck (like if the browser was still waiting for hours for the page to load) – Basj Feb 24 '16 at 14:28
  • Thanks, what about if you use chrome instead of firefox? – alecxe Feb 24 '16 at 14:29
  • Does the website allow for subsequent requests? It may be helpful to see the URLs and learn about the sleeps you inserted. – flaschbier Feb 24 '16 at 14:32
  • I tried to reproduce it a lot in the meantime, and I corrected my question : it's not a crash, it's only "stuck". – Basj Feb 24 '16 at 14:32
  • The question is : isn't there *another system* than having the code to launch Firefox / Chrome and then asking the browser to take the screenshot? It is a bit dirtyish. Wouldn't it possible to have a 100% command-line solution (not relying on an external browser) ? – Basj Feb 24 '16 at 14:34
  • You do not employ an external browser, but you will have to employ a browser engine that does the JavaScript execution and the HTML/CSS rendering for you. This is the headless browser, in your case Selenium. If you don't like Selenium, you can use e.g. PhantomJS, but without a packaged browser engine... – flaschbier Feb 24 '16 at 14:42

1 Answers1

1

I created a library called pywebcapture that wraps selenium that will do just that:

pip install pywebcapture

Once you install with pip, you can do the following to easily get full size screenshots:

# import modules
from pywebcapture import loader, driver

# load csv with urls
csv_file = loader.CSVLoader("csv_file_with_urls.csv", has_header_bool, url_column, optional_filename_column)
uri_dict = csv_file.get_uri_dict()

# create instance of the driver and run
d = driver.Driver("path/to/webdriver/", output_filepath, delay, uri_dict)
d.run()

Enjoy!

https://pypi.org/project/pywebcapture/

Token Joe
  • 177
  • 1
  • 9