Python Selenium-我想下载嵌入在网页中的pdf

问题描述

该网页具有登录凭据,登录后我想下载嵌入在网页中的PDF。我能够使用Selenium和

导航到屏幕
# find pdf url
pdf_url = driver.find_element_by_tag_name('iframe').get_attribute("src")
# load page with pdf
driver.get(pdf_url)
# download file
time.sleep(10)
#download = driver.find_element_by_xpath('//*[@id="download"]')
#download.click()

打印(“处理完成”)

enter image description here

直到这一步,它执行得很好,并将aspx页面与pdf一起显示。但是,当我下载时,我的pdf损坏了。当我将鼠标悬停在r.content上时,它具有登录页面信息而不是PDF。在下载pdf时需要帮助

这是Inspect源代码。我已经屏蔽了src Link

<embed id="plugin" type="application/x-google-chrome-pdf" src="https://docview.aspx?i=20198890&amp;docId=00000&amp;c=tttt&amp;s=0000000" stream-url="chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/0000000" headers="Content-Length: 379726
Content-Type: application/pdf
Date: Fri,18 Sep 2020 06:21:24 GMT
Server: Microsoft-IIS/8.5
X-Powered-By: ASP.NET
" background-color="0xFF525659" top-toolbar-height="56" javascript="allow" full-frame="">

# List of PDFs already downloaded

REQUEST_URL = pdf_url

pdfName = "Test.pdf"
            
          

     downloadlocation = pdfdownload + pdfName

     with requests.Session() as session:
     r = session.get(REQUEST_URL)
     open(downloadlocation,'wb').write(r.content)

[Initial Output of ASPX Page that has pdf][1]
  [1]: https://i.stack.imgur.com/QFeSd.png

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)