Python調用Chrome下載文件


1. 查看Chrome版本

用Chrome打開地址:chrome://version/

2. 打開ChromeDriver網站下載配套版本

http://chromedriver.storage.googleapis.com/index.html

 

3. 利用Python腳本驅動ChromeDriver遍歷下載地址

腳本代碼如下:

if __name__ == '__main__':
    CURRENT_PATH = os.path.split(os.path.realpath(__file__))[0] + os.sep + 'download'
    print(CURRENT_PATH)
    chrome_driver = '/Applications/Google Chrome.app/Contents/MacOS/chromedriver'
    target_url = 'http://bestor.auto.amap.com:14002/bestor_web/crashLog/show?downloadFlag=true&url=/bestor/data/save_log/4.5.0.600444/20200419000000000/dump_4.5.0.600444_57bc4576cb757b5cb15a3546899cc44c_20200421221956248_73593.zip'

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from time import sleep

    option = Options()
    option.add_experimental_option("prefs", {
        "download.default_directory": CURRENT_PATH,  # 默認下載路徑
        "profile.default_content_settings.popups": 1,  # 設置為0禁止彈出窗口
        # "profile.managed_default_content_settings.images": 2 #不加載圖片的情況下,可以提升速度
    })
    option.add_argument("--window-size=1280,800")  # 窗口大小

    # 靜默執行
    # option.add_argument("--headless")
    option.add_experimental_option("excludeSwitches", ["enable-automation"])
    option.add_experimental_option('useAutomationExtension', False)

    driver = webdriver.Chrome(executable_path=chrome_driver, options=option)

    login = False
    for url in url_set:    # 已經准備好了的url列表
        driver.get(url)
        print('DOWNLOAD: %s' % url)
        if not login:      # 首次訪問要有個身份驗證的過程時間長一點
            sleep(20)
       login = True
else: sleep(2) print('All done !') driver.quit()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM