简体   繁体   English

如何使用 Selenium 和 python 下载 MHTML 网页?

[英]How to download a MHTML webpage using Selenium with python?

I want to download a webpage in.mhtml format using selenium with python.我想使用 selenium 和 python 下载 .mhtml 格式的网页。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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