推荐答案
将Selenium
与特定的ChromeProfile
结合使用,您可以使用以下代码下载嵌入式pdf:
Using Selenium
with a specific ChromeProfile
you can download embedded pdfs using the following code:
代码:
def download_pdf(lnk):
from selenium import webdriver
from time import sleep
options = webdriver.ChromeOptions()
download_folder = "C:\\"
profile = {"plugins.plugins_list": [{"enabled": False,
"name": "Chrome PDF Viewer"}],
"download.default_directory": download_folder,
"download.extensions_to_open": ""}
options.add_experimental_option("prefs", profile)
print("Downloading file from link: {}".format(lnk))
driver = webdriver.Chrome(chrome_options = options)
driver.get(lnk)
filename = lnk.split("/")[4].split(".cfm")[0]
print("File: {}".format(filename))
print("Status: Download Complete.")
print("Folder: {}".format(download_folder))
driver.close()
当我调用此函数时:
download_pdf("http://www.equibase.com/premium/eqbPDFChartPlus.cfm?RACE=1&BorP=P&TID=ALB&CTRY=USA&DT=06/17/2002&DAY=D&STYLE=EQB")
多数民众赞成在输出:
>>> Downloading file from link: http://www.equibase.com/premium/eqbPDFChartPlus.cfm?RACE=1&BorP=P&TID=ALB&CTRY=USA&DT=06/17/2002&DAY=D&STYLE=EQB
>>> File: eqbPDFChartPlus
>>> Status: Download Complete.
>>> Folder: C:\
查看特定的个人资料:
profile = {"plugins.plugins_list": [{"enabled": False,
"name": "Chrome PDF Viewer"}],
"download.default_directory": download_folder,
"download.extensions_to_open": ""}
它禁用Chrome PDF Viewer
插件(将pdf嵌入网页),将默认下载文件夹设置为在download_folder
变量中定义的文件夹,并设置不允许Chrome自动打开任何扩展程序.
It disables the Chrome PDF Viewer
plugin (that embedds the pdf at the webpage), set the default download folder to the folder defined at download_folder
variable and sets that Chrome isn't allowed to open any extensions automatically.
之后,当您打开所谓的内部链接"时,您的网络驱动程序会自动将.pdf
文件下载到download_folder
.
After that, when you open the so called "Internal link" your webdriver will automatically download the .pdf
file to the download_folder
.
这篇关于Python下载嵌入页面的PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!