簡體   English   中英

使用 Selenium/Python 下載嵌入的 PDF?

[英]Download embedded PDF using Selenium/Python?

我已經嘗試了在這個網站上發布的一些解決方案,但我仍然無法使這件事起作用。 我必須從安全的網站獲取 PDF。 我能夠一直到達具有創建 PDF 按鈕的頁面,但我找不到可以讓我下載 PDF 的代碼。 這是我到目前為止所得到的,非常感謝任何幫助!

    from selenium import webdriver
    from selenium.webdriver.common.by import By

    driver = webdriver.Chrome()

    driver.get("https://service.medical.barco.com/server/jsp/login")

    username = driver.find_element_by_name('j_username')
    password = driver.find_element_by_name('j_password')

    username.send_keys("XXX")
    password.send_keys("XXX")

    driver.find_element_by_css_selector('[value="Log on"]').click()

    ##makes the PDF and shows it in the Google PDF viewer

    url = "https://service.medical.barco.com/server/spring/jsp/workstation/complianceCheckDetailReport/?displayId=932610524&date=1598328417477"

    driver.get(url)

    driver.find_element_by_class_name('href-button').click()

    ##This is probably unnecessary but I thought a direct link to the created PDF could give me a variable I could then download

    pdf = "https://service.medical.barco.com/server/spring/jsp/workstation/complianceCheckDetailReport/jasper/report.pdf?format=pdf&displayId=932610524&date=1598328417477"

    driver.get(pdf)

一旦您獲得 PDF,Chromium/Google Chrome 很有可能會在其基於 PDF.js 的查看器中打開相同的文件。 為了解決這個問題並“下載”PDF,請嘗試在創建Chrome()實例時傳遞具有以下配置文件屬性的ChromeOptions() Chrome()實例,如下所示:

profile = {
    'download.prompt_for_download': False,
    'download.default_directory': '/path/to/download/the/pdf',
    'download.directory_upgrade': True,
    'plugins.always_open_pdf_externally': True,
}
options = webdriver.ChromeOptions()
options.add_experimental_option('prefs', profile)
driver = webdriver.Chrome(options=options)

附帶說明一下,您始終可以使用requests模塊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM