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 AsNew 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
EndWithSet 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
OptionExplicitPublicSub 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"))
EndWithEndWithWith ThisWorkbook.Worksheets("Sheet1")
.Cells(1, 1) = results(0)
Dim file AsString
file = DownloadFile("C:\Users\User\Desktop\", results(1)) 'your path to download fileWith .Pictures.Insert(file)
.Left = ThisWorkbook.Worksheets("Sheet1").Cells(1, 2).Left
.Top = ThisWorkbook.Worksheets("Sheet1").Cells(1, 2).Top
.Width = 75
.Height = 100
.Placement = 1EndWithEndWith
Kill file
EndSub
Post a Comment for "How To Pull The Image And Title Of The Product From Amazon?"