pythonimgkit

Taking Web screenshot using imgkit


I was trying to take screenshots using imgkit as follows,

options = {
    'width': 1000,
    'height': 1000
}
imgkit.from_url('https://www.ozbargain.com.au/', 'out1.jpg', options=options)

What I am getting is enter image description here

The actual look is a bit different. Possibly this is due to javascript not being executed [its a guess]. Could you please tell me a way how can I do this with imgkit. Any suggested library would be helpful too.


Solution

  • You could use Selenium to control web browser Chrome or Firefox which can run JavaScript and browser has function to take screenshot. But JavaScript may display windows with messages which you may have to close using click() in code - but you would have to find manually (in DevTools in browser) class name, id or other values which helps Selenium to recognize button on page.

    from selenium import webdriver
    from time import sleep
    
    #driver = webdriver.Firefox()
    driver = webdriver.Chrome()
    driver.get('https://www.ozbargain.com.au/')
    driver.set_window_size(1000, 1000)
    sleep(2)
    
    # close first message
    driver.find_element_by_class_name('qc-cmp-button').click()
    sleep(1)
    
    # close second message with details
    driver.find_element_by_class_name('qc-cmp-button.qc-cmp-save-and-exit').click()
    sleep(1)
    
    driver.get_screenshot_as_file("screenshot.png")
    #driver.quit()
    

    enter image description here


    Eventually you could use PyAutoGUI or mss to take screenshot of full desktop or some region on desktop.