pythonweb-scrapingscreenshotscreen-scrapingwebpage-screenshot

Capturing a specific webpage or url (python)


Im new in python and opencv and i'm making a project that captures my whole window , eventually i cope up an idea that i want to capture a specific webpage not my whole window . It is possible to Capture a webpage/url that's run on my computer? (for example i want to capture google url or Stack OverFlow )


(The Code is more like Screen Recorder) Heres my Code :

import numpy as np
import cv2 as cv

import webbrowser
import pyautogui
from PIL import ImageGrab
from time import time




loop_time = time()




while True :



screenshot = pyautogui.screenshot()
#slicing the frame to make readable in opencv
screenshot = np.array(screenshot)

# Our operations on the frame come here
screenshot = cv.cvtColor(screenshot, cv.COLOR_BGRA2BGR)


#Displaying Text on the screen
# text = "Screen Shot"
# coordinates = (100,100)
# font = cv.FONT_HERSHEY_SIMPLEX
# fontScale = 1
# color = (255,0,255)
# thickness = 2

# screenshot = cv.putText(screenshot,text, coordinates, font, fontScale, color, thickness, cv2.LINE_AA)

cv.imshow('Capture Screen', screenshot)

# debuging fps

print(f'FPS { 1/(time()-loop_time):.0f}')
loop_time = time()

# wait time
keyCode = cv.waitKey(1)

#use esc or q key to exit window
if keyCode == 27 or keyCode == ord('q'):
    print('Window is Closed')
    cv.destroyAllWindows()
    break

Solution

  • You could use PyGetWindow to select a specific window/app and get a screenshot of it like so:

    import pyautogui
    import pygetwindow as gw
    
    gw.getWindowsWithTitle('Firefox')
    active_window.activate()
    pyautogui.screenshot().save('test.png')
    

    Note: if you're working on a mac then the above will probably not work though.