返回总目录页

将自己喜欢的网页保存为单个文件包括图片(mhtml文件)

 

from selenium import webdriver

driver = webdriver.Chrome(r'C:\chromedriver_win32\chromedriver.exe')
driver.get('https://baike.baidu.com/item/宇宙/31801')

# 1. 执行 Chome 开发工具命令,得到mhtml内容
res = driver.execute_cdp_cmd('Page.captureSnapshot', {})

# 2. 写入文件
with open('H:\宇宙.mhtml', 'w', newline='') as f:   # 根据5楼的评论,添加newline=''
    f.write(res['data'])

执行程序,打开谷歌,访问“宇宙”百度百科词条页面,保存文件。关闭浏览器

效果:

 

posted @ 2021-09-19 18:53  马昌伟  阅读(971)  评论(0编辑  收藏  举报
博主链接地址:https://www.cnblogs.com/machangwei-8/