Python下载PDF嵌入页面

时间:2017-04-18 11:10:58

标签: python pdf web-scraping

我有这个链接:

我想下载嵌入式PDF。

我尝试过window.addEventListener('load', function() { urllib的常规方法,但它们无效。

request

此外,我还试图找到pdf的原始链接,但它也没有用。

内部链接:

1 个答案:

答案 0 :(得分:3)

Selenium与特定ChromeProfile一起使用,您可以使用以下代码下载嵌入式pdf:

<强>代码:

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:\

enter image description here

查看具体资料:

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不允许打开任何自动扩展。

之后,当你打开所谓的&#34;内部链接&#34;您的webdriver会自动将.pdf文件下载到download_folder