Skip to content

Instantly share code, notes, and snippets.

@mezhgano
Created May 1, 2023 14:58
Show Gist options
  • Save mezhgano/bd9fee908378ee87589b727906da55db to your computer and use it in GitHub Desktop.
Save mezhgano/bd9fee908378ee87589b727906da55db to your computer and use it in GitHub Desktop.
Save .mhtml web archive using playwright
from playwright.sync_api import sync_playwright
def save_mhtml(path: str, text: str):
with open(path, mode='w', encoding='UTF-8', newline='\n') as file:
file.write(text)
def save_page(url: str, path: str):
with sync_playwright() as playwright:
browser = playwright.chromium.launch(headless=False)
page = browser.new_page()
page.goto(url)
client = page.context.new_cdp_session(page)
mhtml = client.send("Page.captureSnapshot")['data']
save_mhtml(path, mhtml)
browser.close()
if __name__ == '__main__':
save_page('https://example.com/', 'example.mhtml')
@ixuuux
Copy link

ixuuux commented May 5, 2023

good job

@lijialing888
Copy link

amazing

@dallarwww
Copy link

cooooooooooool

@wjudho
Copy link

wjudho commented Apr 3, 2024

the page is perfect, but how do i parse them?

@mezhgano
Copy link
Author

the page is perfect, but how do i parse them?

Hello, you can parse information from page without downloading it.
For Python most common library for web parsing is BeautifulSoup, just search for "Python web parsing tutorial" and you'll be good.

@lzy37ld
Copy link

lzy37ld commented May 13, 2024

@mezhgano Hi, I have searched for several public resources to parse it but all of them are talking about extract to html while ignoring all other resources like images and css. Do you know how to parse all resources out as "Save as" operation in the Chrome by clicking right mouse?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment