How To Pull The Image And Title Of The Product From Amazon?
I am trying to make a list of products based on the unique product codes of Amazon. For example: https://www.amazon.in/gp/product/B00F2GPN36 Where B00F2GPN36 is the unique code. I
Solution 1:
There is no such thing as html.getElementsById("productTitle")
in vba. ID's are always unique, so it should be html.getElementById("productTitle")
. Run the following script to get them:
Sub ParseHtml()
Dim IE As New InternetExplorer, elem As Object
Dim Html As HTMLDocument, imgs As Object
With IE
.Visible = False
.navigate "https://www.amazon.in/gp/product/B00F2GPN36"
While .Busy Or .readyState < 4: DoEvents: Wend
Set Html = .document
End With
Set elem = Html.getElementById("productTitle")
Set imgs = Html.getElementById("landingImage")
Sheets(1).Cells(1, 1) = elem.innerText
Sheets(1).Cells(1, 1).Offset(0, 1) = imgs.getAttribute("data-old-hires")
End Sub
Solution 2:
Faster would be to use xhr and avoid browser and write out results from an array to sheet
Option Explicit
Public Sub GetInfo()
Dim html As HTMLDocument, results()
Set html = New HTMLDocument
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", "https://www.amazon.in/gp/product/B00F2GPN36", False
.send
html.body.innerHTML = .responseText
With html
results = Array(.querySelector("#productTitle").innerText, .querySelector("#landingImage").getAttribute("data-old-hires"))
End With
End With
With ThisWorkbook.Worksheets("Sheet1")
.Cells(1, 1) = results(0)
Dim file As String
file = DownloadFile("C:\Users\User\Desktop\", results(1)) 'your path to download file
With .Pictures.Insert(file)
.Left = ThisWorkbook.Worksheets("Sheet1").Cells(1, 2).Left
.Top = ThisWorkbook.Worksheets("Sheet1").Cells(1, 2).Top
.Width = 75
.Height = 100
.Placement = 1
End With
End With
Kill file
End Sub
Post a Comment for "How To Pull The Image And Title Of The Product From Amazon?"