目前我正在尝试在一个打开的网页上搜索一些png图像,并将它们导入到我的Excel工作簿上的一个选项卡中的不同单元格中。
我现有的宏浏览一个网页,该网页指示将显示哪些图片以及有多少图片。
我正在尝试编写宏来搜索打开的网页并将图像导入excel。
每次运行程序时都无法识别图像数。但是每个图像都在以下html代码中:
img src="/files/exercises/31a_3 - trunk stability rotation kneex flexed.still002.194x146.png" title="Step 2"
虽然文件名会改变。我正在寻找标题为“步骤2”的所有图像
并将所有这些图片导入excel。 目前,我可以通过以下代码成功找到网址:
max = 100
For i = 0 To max
For Each Elem In ie.Document.getElementsByTagName("img")
If ie.Document.getElementsByTagName("img").Item(i).getAttribute("title") = "Step 2" Then
Worksheets("Sheet4").Range("A1") = "http://functionalmovement.com" & ie.Document.getElementsByTagName("img").Item(i).getAttribute("src")
Exit For
Exit For
End If
Next
Next
End Sub
的问题:
如何搜索整个页面并将第一个发现放在特定单元格中,第二个发现放在另一个单元格中,依此类推,直到找不到更多。
现在有一种方法让图像本身出现,因为每个图像都知道完整的网址。
答案 0 :(得分:1)
找到我要找的东西。此代码搜索title =“Step 1”的图像。然后我下载这些图像。
Sub findimageA()
Dim strURL As String
Dim ie As Object
Dim Elem
Dim X
Set ie = CreateObject("InternetExplorer.Application")
strURL = "http://blahblah"
ie.Navigate strURL
ie.Visible = True
Do While (ie.Busy Or ie.READYSTATE <> 4)
DoEvents
Loop
For Each Elem In ie.Document.getElementsByTagName("img")
If Elem.getAttribute("title") = "Step 1" Then
Select Case X
Case Is = 0
pic1aURL = "http://functionalmovement.com" & Elem.getAttribute("src")
pic1aName = Elem.getAttribute("src")
pic1aName1 = Replace(pic1aName, "/files/exercises/", "")
Case Is = 1
pic2aURL = "http://functionalmovement.com" & Elem.getAttribute("src")
pic2aName = Elem.getAttribute("src")
pic2aName1 = Replace(pic2aName, "/files/exercises/", "")
Case Is = 2
End Select
X = X + 1
End If
Next
If Not IsEmpty(picaName) Then
DownloadFile
End If
End Sub