【发布时间】:2019-03-24 04:58:04
【问题描述】:
由于 VBA 中的 IE 自动化,我正在尝试从网站(职位发布)下载 PDF。我无法生成单个 PDF。
在网页上手动执行此操作并在 pdf 图标上执行“目标另存为”会给我一个有效的 PDF。
我目前拥有的代码(网址是公开的,我随机选择了优惠)。
Private Declare Function DownloadFilefromURL Lib "urlmon" _
Alias "URLDownloadToFileA" _
(ByVal pCaller As Long, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReserved As Long, _
ByVal lpfnCB As Long) As Long
Private Const ERROR_SUCCESS As Long = 0
Private Const BINDF_GETNEWESTVERSION As Long = &H10
Public Function DownloadFile(SourceUrl As String, LocalFile As String) As Boolean
DownloadFile = DownloadFilefromURL(0&, SourceUrl, LocalFile, BINDF_GETNEWESTVERSION, 0&) = ERROR_SUCCESS
End Function
Sub TestSavePDF()
Dim oNav As SHDocVw.InternetExplorer
Dim oDoc As MSHTML.HTMLDocument
Dim MyURL As String
Set oNav = New SHDocVw.InternetExplorer
oNav.Visible = True
'Test Altays Client A (Banque de France)
MyURL = "https://www.recrutement.banque-france.fr/detail-offre/?NoSource=16001&NoSociete=167&NoOffre=2036788&NoLangue=1"
'Test Altays Client B (Egis)
' MyURL = "https://www.altays-progiciels.com/clicnjob/FicheOffreCand.php?PageCour=1&Liste=Oui&Autonome=0&NoOffre=2037501&RefOffrel=&NoFaml=0&NoParam1l=0&NoParam2l=0&NoParam3l=0&NoParam133l=0&NoParam134l=0&NoParam136l=0&NoEntite1=0&NoEntite=&NoPaysl=0&NoRegionl=0&NoDepartementl=0&NoTableOffreLieePl=0&NoTableOffreLieeFl=0&NoNivEtl=0&NoTableCCl=0&NoTableCC2l=0&NoTableCC3l=0&NoTableOffreUnl=0&NoTypContratl=0&NoTypContratProl=0&NoStatutOffrel=&NoUtilisateurl=&RechPleinTextel=#ancre3"
oNav.navigate MyURL
'link provided to download the job offer in PDF. when clicked the PDF opens in a new tab
MyURL = "https://www.altays-progiciels.com/clicnjob/ExportPDFFront.php"
DownloadFile MyURL, "C:\[...Path...]\test.pdf"
End Sub
【问题讨论】:
标签: html vba internet-explorer web-scraping pdf-generation