Home how to use getelementsbytagname to extract LI value
Reply: 1

how to use getelementsbytagname to extract LI value

wei h
1#
wei h Published in 2018-01-12 18:32:54Z

I am trying to search for a product on amazon.com and extract the asin for the item. The asin is hidden in the li under data-asin tag. my excel macro currently runs through all the search tag options and checks amazon 1 by 1 and gathers the product price. But I need this function to also gather the asin for each item

Code Below:

Sub SearchGoogle()
Dim ie As Object
Dim form As Variant
Dim button As Variant
Dim LR As Integer
Dim var As String
Dim var1 As Object
Dim var2 As Object
LR = Cells(Rows.Count, 1).End(xlUp).Row
For x = 2 To LR
var = Cells(x, 1).Value
Set ie = CreateObject("internetexplorer.application")
ie.Visible = True
 With ie
.Visible = True
.navigate "https://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords=" & Range("azAsin")
While Not .readyState = READYSTATE_COMPLETE
Wend
End With
'Wait some to time for loading the page
While ie.Busy
DoEvents
Wend
Application.Wait (Now + TimeValue("0:00:02"))

While ie.Busy
DoEvents
Wend

Application.Wait (Now + TimeValue("0:00:02"))
'Set var1 = ie.document.getElementsByClassName("sx-price-whole")
'Cells(x, 2).Value = var1(0).textContent
Set var2 = ie.document.getElementById("result_0").getelementsbytagname("data-asin")
MsgBox var2(0).textContent
'Cells(x, 3).Value = var2().innerText
ie.Quit
Set ie = Nothing
Next x
End Sub

HTML CODE from amazon.com when i search for a specific item:

<li id="result_0" data-result-rank="0" data-asin="B006RN77MI" data-action="sx-detail-display-trigger" class="s-result-item s-result-card-for-container a-declarative celwidget  "><div class="s-item-container" style="height: 367px;"><div class="a-row a-spacing-top-micro a-spacing-micro"><div class="a-row sx-badge-region"><div class="a-row a-spacing-large"></div></div></div><div class="a-row a-spacing-base"><div aria-hidden="true" class="a-column a-span12 a-text-center s-position-relative"><a class="a-link-normal a-text-normal" href="https://www.amazon.com/Ameda-Platinum-Breast-Grounded-Power/dp/B006RN77MI/ref=sr_1_1_a_it?ie=UTF8&amp;qid=1515781416&amp;sr=8-1&amp;keywords=B006RN77MI"><img src="https://images-na.ssl-images-amazon.com/images/I/41NrM6wVJTL._AC_US200_.jpg" srcset="https://images-na.ssl-images-amazon.com/images/I/41NrM6wVJTL._AC_US200_.jpg 1x, https://images-na.ssl-images-amazon.com/images/I/41NrM6wVJTL._AC_US300_FMwebp_QL65_.jpg 1.5x, https://images-na.ssl-images-amazon.com/images/I/41NrM6wVJTL._AC_US400_FMwebp_QL65_.jpg 2x, https://images-na.ssl-images-amazon.com/images/I/41NrM6wVJTL._AC_US500_FMwebp_QL65_.jpg 2.5000x" width="200" height="200" alt="Product Details" class="s-access-image cfMarker" data-search-image-load=""></a><div class="a-section a-spacing-none a-text-center"></div></div></div><div class="a-row a-spacing-none"><div class="a-row a-spacing-mini"><a class="a-link-normal s-access-detail-page  s-color-twister-title-link a-text-normal" title="Ameda Platinum Breast Pump with Grounded Power Cord" href="https://www.amazon.com/Ameda-Platinum-Breast-Grounded-Power/dp/B006RN77MI/ref=sr_1_1_a_it?ie=UTF8&amp;qid=1515781416&amp;sr=8-1&amp;keywords=B006RN77MI"><h2 data-attribute="Ameda Platinum Breast Pump with Grounded Power Cord" data-max-rows="0" class="a-size-base s-inline  s-access-title  a-text-normal">Ameda Platinum Breast Pump with Grounded Power Cord</h2></a></div><div class="a-row a-spacing-none"><a class="a-link-normal a-text-normal" href="https://www.amazon.com/Ameda-Platinum-Breast-Grounded-Power/dp/B006RN77MI/ref=sr_1_1?ie=UTF8&amp;qid=1515781416&amp;sr=8-1&amp;keywords=B006RN77MI"><span class="a-offscreen">$1,995.00</span><span aria-hidden="true" class="a-color-base sx-zero-spacing"><span class="sx-price sx-price-large">
                        <sup class="sx-price-currency">$</sup>
                        <span class="sx-price-whole">1,995</span>
                        <sup class="sx-price-fractional">00</sup>
                    </span>
                </span></a></div><div class="a-row a-spacing-none"><span class="a-size-small a-color-price">Only 1 left in stock - order soon.</span></div><div class="a-row a-spacing-top-mini a-spacing-none"><span name="B006RN77MI">
user3561813
2#
user3561813 Reply to 2018-01-12 19:21:08Z

In your case, the asin is not a tag name, but rather an attribute. In order to return the value, you can use code such as:

strAsin = ie.document.getElementById("result_0").getAttrubute("data-asin")

Note: this will return a string, not an object.

To prevent unsightly errors, it may make sense to first test to confirm that the "data-asin" attribute exists. Code such as the following accomplishes that:

If ie.document.getElementById("result_0").hasAttrubute("data-asin") then
    strAsin = ie.Document.getElementById("result_0").getAttribute("data-asin")
Else
     'Do something if attribute doesn't exist
End if 
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.358878 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO