简体   繁体   中英

How to download a MHTML webpage using Selenium with python?

I want to download a webpage in.mhtml format using selenium with python. I am using the following code:

driver = webdriver.Chrome()
driver.get("http://www.yahoo.com")
with open("/path/to/page_source.mhtml", "w", encoding="utf-8") as f:
    f.write(driver.page_source)

It saved the page but the page just had source code. Cannot view the original content on the page. Any suggestions?

Thanks Karan

You can try this:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager

options = webdriver.ChromeOptions()
options.add_argument('--save-page-as-mhtml')
driver = webdriver.Chrome(options = options, service=ChromeService(ChromeDriverManager().install()))

driver.get("http://www.yahoo.com")

with open("page_source.mhtml", "w", encoding="utf-8") as f:
    f.write(driver.page_source)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM