В интернете нашёл такое решение. Через selenium и браузер Chrome. Думаю, основываясь на этом, можно переделать под этот случай.
Я даже нашёл скрипт для автоматической установки драйвера
https://pypi.org/project/chromedriver-autoinstaller/
# pip install selenium
import os
from pathlib import Path
from platform import system
from urllib.parse import urlparse
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
options = Options()
options.add_argument("--headless")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument("--disable-blink-features=AutomationControlled")
exec_path = os.path.join(os.getcwd(), 'driver', 'chromedriver.exe') if system() == "Windows" else \
os.path.join(os.getcwd(), 'driver', 'chromedriver')
driver = webdriver.Chrome(options=options, service=Service(log_path=os.devnull, executable_path=exec_path))
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
'source': '''
delete window.cdc_adoQpoasnfa76pfcZLmcfl_Array;
delete window.cdc_adoQpoasnfa76pfcZLmcfl_Promise;
delete window.cdc_adoQpoasnfa76pfcZLmcfl_Symbol;
'''
})
def screen_sait(url) -> None:
path = str(urlparse(url).hostname).replace(Path(str(urlparse(url).hostname)).suffix, ".png")
driver.get(url)
s = lambda x: driver.execute_script('return document.body.parentNode.scroll' + x)
driver.set_window_size(s('Width'), s('Height'))
driver.find_element(By.TAG_NAME, 'body').screenshot(path)
driver.close()
driver.quit()
screen_sait('https://codeby.net/')