【发布时间】:2020-10-29 11:43:30
【问题描述】:
我正在尝试获取页面上图片的 src 属性,然后下载该图片,这是我的代码
picURL = "https://iofferman.x.yupoo.com/33269655?uid=1"
Set htmlPic = GetHTML(picURL)
Debug.Print Replace(htmlPic.querySelector(".viewer__imgwrap img").getAttribute("src"), "about:", "https:")
Dim myPic As String
myPic = Replace(htmlPic.querySelector(".viewer__imgwrap img").getAttribute("src"), "about:", "https:")
'URLDownloadToFile 0, myPic, ThisWorkbook.Path & "\" & picID & ".jpg", 0, 0
Call SaveWebFile(myPic, ThisWorkbook.Path & "\" & picID & ".jpg")
这个和代码有关
Function GetHTML(ByVal sURL As String) As HTMLDocument
Dim http As MSXML2.XMLHTTP60, html As MSHTML.HTMLDocument
Set http = New MSXML2.XMLHTTP60
Set html = New MSHTML.HTMLDocument
With http
.Open "Get", sURL, False
.send
html.body.innerHTML = .responseText
End With
Set GetHTML = html
End Function
这是下载图片的UDF
Function SaveWebFile(ByVal sFile$, ByVal sPath$) As Boolean
Dim f&, oResp() As Byte
With CreateObject("MSXML2.ServerXMLHTTP")
.Open "GET", sFile, False
.send
Do While (.readyState <> 4): DoEvents: Loop
oResp = .responseBody
End With
f = FreeFile
If Dir(sPath) <> "" Then Kill sPath
Open sPath For Binary As #f
Put #f, , oResp
Close #f
End Function
我可以获取图片的链接,例如:https://photo.yupoo.com/iofferman/2d03c9b8/326e8e47.jpg,但是当导航到它时,有时我可以正确获取图片,有时会给出不正确的输出,例如http://adc.yupoo.com/res/703.gif。如何正确下载图片?
【问题讨论】:
-
我不得不删除我的答案。链接
https://iofferman.x.yupoo.com/33269655?uid=1不是图片链接。这是一个网页链接。如果你查看网页的来源,你会看到图片的实际链接是"//photo.yupoo.com/iofferman/2d03c9b8/326e8e47.jpg"你可以看到在页面的来源<a id="viewer__origin_img" target="_blank" class="button small onlydesktop" href="//photo.yupoo.com/iofferman/2d03c9b8/326e8e47.jpg">Original image</a>你需要寻找锚文本Original image然后提取从那里链接 -
这就是问题所在。当您取出此链接并尝试查看图像时(有时有效,有时给我另一张被认为不正确的图片)
-
您需要查找锚文本
Original image,然后从那里提取链接。这样,您将始终获得正确的链接 -
什么锚文本?你能把这张图片的链接提取出来试试吗?不是正确的链接
https//photo.yupoo.com/iofferman/2d03c9b8/326e8e47.jpg吗? -
Isn't the correct link https//photo.yupoo.com/iofferman/2d03c9b8/326e8e47.jpg是的。这就是我在上面发布的第一条评论中提到的。在浏览器中打开链接https://iofferman.x.yupoo.com/33269655?uid=1,然后右键单击它并单击View Source。在那里你可以看到网页的来源。搜索文本Original image。在那里你可以看到实际的链接。如果您可以提供几个链接,我可以建议一个代码从中提取链接
标签: excel vba xmlhttprequest