Using Selenium in Python to save a webpage on Firefox

前端 未结 4 2134
野的像风
野的像风 2021-01-01 17:47

I am trying to use Selenium in Python to save webpages on MacOS Firefox.

So far, I have managed to click COMMAND + S

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-01 18:10

    This is a complete, working example of the answer RemcoW provided:

    You first have to install a webdriver, e.g. pip install selenium chromedriver_installer.

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    # core modules
    import codecs
    import os
    
    # 3rd party modules
    from selenium import webdriver
    
    
    def get_browser():
        """Get the browser (a "driver")."""
        # find the path with 'which chromedriver'
        path_to_chromedriver = ('/usr/local/bin/chromedriver')
        browser = webdriver.Chrome(executable_path=path_to_chromedriver)
        return browser
    
    
    save_path = os.path.expanduser('~')
    file_name = 'index.html'
    browser = get_browser()
    
    url = "https://martin-thoma.com/"
    browser.get(url)
    
    complete_name = os.path.join(save_path, file_name)
    file_object = codecs.open(complete_name, "w", "utf-8")
    html = browser.page_source
    file_object.write(html)
    browser.close()
    

提交回复
热议问题