图片的Html标签,

时间:2015-02-28 23:07:08

标签: html vba parsing

我想从亚马逊解析图片链接的网址,但由于我是新手,我不确定它的标记名。

我有一个使用的vba代码 设置myLinks = html.getElementsByTagName(" img") 然而,要解析,找不到图片链接。

你能帮助我吗?

谢谢。

代码:

Sub GetAboutUsLinks2()
'First define all the variables

Dim ie As Object
Dim html As Object
Dim myLinks As Object
Dim myLink As Object
Dim result As String
Dim myURL As String
Dim LastRow As Integer

Set ie = CreateObject("InternetExplorer.Application")

LastRow = Sheet1.Cells(Rows.Count, "a").End(xlUp).Row
For i = 2 To LastRow
myURL = Sheet1.Cells(i, 1).Value
ie.navigate myURL
ie.Visible = True

While ie.readyState <> 4
DoEvents
Wend

result = ie.document.body.innerHTML
Set html = CreateObject("htmlfile")
html.body.innerHTML = result

Set myLinks = html.getElementsByTagName("img").
For Each myLink In myLinks
If Right$(myLink, 4) = ".jpg" Then
Sheet1.Cells(i, "B").Value = myLinks
Else
Sheet1.Cells(i, "B").Value = "Not found"
End If

Next myLink

If i = LastRow Then
ie.Quit
End If
Next i




End Sub

1 个答案:

答案 0 :(得分:0)

这对我有用。没有必要从IE中获取HTML以进行任何解析:您可以直接使用加载的页面。

Sub GetAboutUsLinks2()
'First define all the variables

Dim ie As Object
Dim myPic As Object
Dim myURL As String
Dim LastRow As Long, i As Long

    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = True

    LastRow = Sheet1.Cells(Rows.Count, "a").End(xlUp).Row

    For i = 2 To LastRow

        myURL = Sheet1.Cells(i, 1).Value
        ie.navigate myURL

        While ie.busy Or ie.readyState <> 4
            DoEvents
        Wend

        Set myPic = ie.document.getElementById("landingImage")
        If Not myPic Is Nothing Then
            Sheet1.Cells(i, "B").Value = myPic.src
        Else
            Sheet1.Cells(i, "B").Value = "Not found"
        End If

    Next i

    ie.Quit
    Set ie = Nothing

End Sub