简体   繁体   English

如何使用Python Selenium下载完整的网页

[英]How to download complete webpage using Python Selenium

I have to write a Python code that will get URL, open a Chrome/Firefox browser using Selenium and will download it as a "Complete Webpage", mean with the CSS assets for example. 我必须编写一个Python代码来获取URL,使用Selenium打开Chrome / Firefox浏览器并将其下载为“完整网页”,例如CSS资产。

I know the basis of using Selenium, like: 我知道使用Selenium的基础,如:

from selenium import webdriver

ff = webdriver.firefox()
ff.get(URL)

ff.close

How can I perform the downloading action (Like clicking automatically in the browser CTRL+S)? 如何执行下载操作(如在浏览器中自动单击CTRL + S)?

You can try following code to get HTML page as file: 您可以尝试使用以下代码将HTML页面作为文件获取:

from selenium import webdriver

ff = webdriver.Firefox()
ff.get(URL)
with open('/path/to/file.html', 'w') as f:
    f.write(ff.page_source)
ff.close

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

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