VBA宏从IE中的链接下载多个文件
我想从链接列表下载多个文件。我找到链接的网站受到保护。这就是为什么我想使用IE(使用当前会话/cookie)。每个链接的目标都是一个xml文件。文件太大,无法打开然后保存。所以我需要直接保存它们(右键单击,将目标另存为) 链接列表如下所示:VBA宏从IE中的链接下载多个文件,vba,internet-explorer,vbscript,right-click,save-as,Vba,Internet Explorer,Vbscript,Right Click,Save As,我想从链接列表下载多个文件。我找到链接的网站受到保护。这就是为什么我想使用IE(使用当前会话/cookie)。每个链接的目标都是一个xml文件。文件太大,无法打开然后保存。所以我需要直接保存它们(右键单击,将目标另存为) 链接列表如下所示: <html> <body> <p> <a href="https://example.com/report?_hhhh=XML"Link A</a><br>> </p> &l
<html>
<body>
<p> <a href="https://example.com/report?_hhhh=XML"Link A</a><br>> </p>
<p> <a href="https://example.com/report?_aaaa=XML"Link B</a><br>> </p>
...
</body>
</html>
您是否有任何想法可以为每个链接自动执行“另存为”
感谢您的帮助。非常感谢,,
Uli下面是我为您的案例改编的一个非常常见的示例,它显示了使用XHR和正则表达式检索网页HTML内容,从中提取所有链接,并下载每个链接的目标文件:
Option Explicit
Sub Test()
' declare vars
Dim sUrl As String
Dim sReqProt As String
Dim sReqAddr As String
Dim sReqPath As String
Dim sContent As String
Dim oLinks As Object
Dim oMatch As Object
Dim sHref As String
Dim sHrefProt As String
Dim sHrefAddr As String
Dim sHrefPath As String
Dim sHrefFull As String
Dim n As Long
Dim aContent() As Byte
' set source URL
sUrl = "https:\\......\links.html"
' process source URL
SplitUrl sUrl, sReqProt, sReqAddr, sReqPath
If sReqProt = "" Then sReqProt = "http:"
sUrl = sReqProt & "//" & sReqAddr & "/" & sReqPath
' retrieve source page HTML content
With CreateObject("Microsoft.XMLHTTP")
.Open "GET", sUrl, False
.Send
sContent = .ResponseText
End With
' parse source page HTML content to extract all links
Set oLinks = CreateObject("Scripting.Dictionary")
With CreateObject("VBScript.RegExp")
.Global = True
.MultiLine = True
.IgnoreCase = True
.Pattern = "<a.*?href *= *(?:'|"")(.*?)(?:'|"").*?>"
For Each oMatch In .Execute(sContent)
sHref = oMatch.subMatches(0)
SplitUrl sHref, sHrefProt, sHrefAddr, sHrefPath
If sHrefProt = "" Then sHrefProt = sReqProt
If sHrefAddr = "" Then sHrefAddr = sReqAddr
sHrefFull = sHrefProt & "//" & sHrefAddr & "/" & sHrefPath
oLinks(oLinks.Count) = sHrefFull
Next
End With
' save each link target into file
For Each n In oLinks
sHref = oLinks(n)
With CreateObject("Microsoft.XMLHTTP")
.Open "GET", sHref, False
.Send
aContent = .ResponseBody
End With
With CreateObject("ADODB.Stream")
.Type = 1 ' adTypeBinary
.Open
.Write aContent
.SaveToFile "C:\Test\" & n & ".xml", 2 ' adSaveCreateOverWrite
.Close
End With
Next
End Sub
Sub SplitUrl(sUrl, sProt, sAddr, sPath)
' extract protocol, address and path from URL
Dim aSplit
aSplit = Split(sUrl, "//")
If UBound(aSplit) = 0 Then
sProt = ""
sAddr = sUrl
Else
sProt = aSplit(0)
sAddr = aSplit(1)
End If
aSplit = Split(sAddr, "/")
If UBound(aSplit) = 0 Then
sPath = sAddr
sAddr = ""
Else
sPath = Mid(sAddr, Len(aSplit(0)) + 2)
sAddr = aSplit(0)
End If
End Sub
选项显式
子测试()
“申报vars
作为字符串的Dim sUrl
Dim sReqProt作为字符串
Dim sReqAddr作为字符串
将sReqPath设置为字符串
像字符串一样模糊的内容
作为对象的模糊OLINK
作为对象的Dim oMatch
朦胧的丝线
像细绳一样的暗淡的碎肉
朦胧的丝线
朦胧的丝线
像细绳一样暗淡的史莱夫
长
Dim aContent()作为字节
'设置源URL
sUrl=“https:\\..\links.html”
'进程源URL
拆分URL sUrl、sReqProt、sReqAddr、sReqPath
如果sReqProt=“”,则sReqProt=“http:”
sUrl=sReqProt&“/”&sReqAddr&“/”&sReqPath
'检索源页面HTML内容
使用CreateObject(“Microsoft.XMLHTTP”)
.打开“GET”,sUrl,False
.发送
sContent=.ResponseText
以
'解析源页面HTML内容以提取所有链接
设置oLinks=CreateObject(“Scripting.Dictionary”)
使用CreateObject(“VBScript.RegExp”)
.Global=True
.MultiLine=True
.IgnoreCase=True
.Pattern=“”
对于.Execute中的每个oMatch(内容)
sHref=oMatch.子匹配(0)
拆分URL sHref、sHrefProt、sHrefAddr、sHrefPath
如果sHrefProt=“则sHrefProt=sReqProt
如果sHrefAddr=“”,则sHrefAddr=sReqAddr
sHrefFull=sHrefProt&“/”&sHrefAddr&“/”&sHrefPath
奥林克斯(奥林克斯数)=史瑞福
下一个
以
'将每个链接目标保存到文件中
在奥林克斯每n
什里夫=奥林克斯(n)
使用CreateObject(“Microsoft.XMLHTTP”)
.打开“GET”,sHref,False
.发送
aContent=.ResponseBody
以
使用CreateObject(“ADODB.Stream”)
.Type=1'adTypeBinary
打开
.写一封信
.SaveToFile“C:\Test\”&n&“.xml”,2'adSaveCreateOverWrite
.结束
以
下一个
端接头
子拆分URL(sUrl、sProt、sAddr、sPath)
'从URL提取协议、地址和路径
暗淡的幻影
aSplit=Split(sUrl,“/”)
如果UBound(aSplit)=0,则
sProt=“”
sAddr=sUrl
其他的
sProt=aSplit(0)
sAddr=aSplit(1)
如果结束
aSplit=Split(sAddr,“/”)
如果UBound(aSplit)=0,则
sPath=sAddr
sAddr=“”
其他的
sPath=Mid(sAddr,Len(aSplit(0))+2)
sAddr=aSplit(0)
如果结束
端接头
这种方法不采用IE自动化。通常IE的Cookie
Microsoft.XMLHTTP
过程足以引用当前会话,因此,如果您的网站没有使用额外的过程来验证和生成链接列表,那么该方法应该适合您。这是一个兔子洞,我已经钻过很多次了。简单的回答是停止让IE充当下载文件的代理。使用xmlHttp对象登录并使用GetResponseHeader收集/返回身份验证,然后使用ADO流保存文件。也许会有帮助。
Option Explicit
Sub Test()
' declare vars
Dim sUrl As String
Dim sReqProt As String
Dim sReqAddr As String
Dim sReqPath As String
Dim sContent As String
Dim oLinks As Object
Dim oMatch As Object
Dim sHref As String
Dim sHrefProt As String
Dim sHrefAddr As String
Dim sHrefPath As String
Dim sHrefFull As String
Dim n As Long
Dim aContent() As Byte
' set source URL
sUrl = "https:\\......\links.html"
' process source URL
SplitUrl sUrl, sReqProt, sReqAddr, sReqPath
If sReqProt = "" Then sReqProt = "http:"
sUrl = sReqProt & "//" & sReqAddr & "/" & sReqPath
' retrieve source page HTML content
With CreateObject("Microsoft.XMLHTTP")
.Open "GET", sUrl, False
.Send
sContent = .ResponseText
End With
' parse source page HTML content to extract all links
Set oLinks = CreateObject("Scripting.Dictionary")
With CreateObject("VBScript.RegExp")
.Global = True
.MultiLine = True
.IgnoreCase = True
.Pattern = "<a.*?href *= *(?:'|"")(.*?)(?:'|"").*?>"
For Each oMatch In .Execute(sContent)
sHref = oMatch.subMatches(0)
SplitUrl sHref, sHrefProt, sHrefAddr, sHrefPath
If sHrefProt = "" Then sHrefProt = sReqProt
If sHrefAddr = "" Then sHrefAddr = sReqAddr
sHrefFull = sHrefProt & "//" & sHrefAddr & "/" & sHrefPath
oLinks(oLinks.Count) = sHrefFull
Next
End With
' save each link target into file
For Each n In oLinks
sHref = oLinks(n)
With CreateObject("Microsoft.XMLHTTP")
.Open "GET", sHref, False
.Send
aContent = .ResponseBody
End With
With CreateObject("ADODB.Stream")
.Type = 1 ' adTypeBinary
.Open
.Write aContent
.SaveToFile "C:\Test\" & n & ".xml", 2 ' adSaveCreateOverWrite
.Close
End With
Next
End Sub
Sub SplitUrl(sUrl, sProt, sAddr, sPath)
' extract protocol, address and path from URL
Dim aSplit
aSplit = Split(sUrl, "//")
If UBound(aSplit) = 0 Then
sProt = ""
sAddr = sUrl
Else
sProt = aSplit(0)
sAddr = aSplit(1)
End If
aSplit = Split(sAddr, "/")
If UBound(aSplit) = 0 Then
sPath = sAddr
sAddr = ""
Else
sPath = Mid(sAddr, Len(aSplit(0)) + 2)
sAddr = aSplit(0)
End If
End Sub