我正打算仅复制图像名称。尝试从下面的代码src =“ images / capchs / 6.png”中拉出它,我想复制“ images / capchs / 6.png”,同样适用于Image2和image3。我的编码不好,有人请帮忙。 提前致谢。 从下面的代码:
PFB我的代码:
Sub SRC_Name()
Dim ie As New InternetExplorer, ws As Worksheet
Dim element As IHTMLElement
Set ws = ThisWorkbook.Worksheets("Unit Data")
With ie
.Visible = True
.Navigate2 "" & Sheets("Home").Range("C3").text
While .Busy Or .readyState < 4: DoEvents: Wend
Dim listings As Object, listing As Object, headers(), results(), r As Long, c As Long, item As Object
headers = Array("Img1", "Img2")
Set listings = .document.getElementsByTagName("td")
Dim html2 As HTMLDocument
Set html2 = New HTMLDocument
ReDim results(1 To listings.Length, 1 To UBound(headers) + 1)
For Each listing In listings
r = r + 1
On Error Resume Next
html2.body.innerHTML = listing.outerHTML
results(r, 1) = html2.querySelector(".cimg1").getAttribute("src")
results(r, 2) = html2.querySelector(".cimg1").getAttribute("src")
On Error GoTo 0
Next
ws.Cells(1, 1).Resize(1, UBound(headers) + 1) = headers
ws.Cells(2, 1).Resize(UBound(results, 1), UBound(results, 2)) = results
.Quit
End With[![enter image description here][1]][1]
答案 0 :(得分:0)
我有代码专家...
Dim ie As New InternetExplorer, ws As Worksheet
Dim element As IHTMLElement
Set ws = ThisWorkbook.Worksheets("Unit Data")
With ie
.Visible = True
.Navigate2 "" & Sheets("Home").Range("C3").text
While .Busy Or .readyState < 4: DoEvents: Wend
Dim listings As Object, listing As Object, headers(), results(), r As Long, c As Long, item As Object
headers = Array("Width", "Length", "Hight/Space Type", "promo", "Reguler Price", "Online Price", "Listing Active", "features")
Set listings = .document.getElementById("cimg1").getElementsByTagName("img")
Dim html As HTMLDocument
Set html = New HTMLDocument
ReDim results(1 To listings.Length, 1 To UBound(headers) + 1)
For Each listing In listings
r = r + 1
html.body.innerHTML = listing.outerHTML
results(r, 1) = html.querySelector("img").getAttribute("src")
On Error GoTo 0
Next
ws.Cells(1, 1).Resize(1, UBound(headers) + 1) = headers
ws.Cells(2, 1).Resize(UBound(results, 1), UBound(results, 2)) = results
.Quit