如何在<a> tag

时间:2018-12-17 14:07:22

标签: html excel vba excel-vba web-scraping

I have a url with the following html part

 <div class="shop cf">
 <a class="shop-logo js-shop-logo" href="/m/3870/GMobile">
 <noscript>

<img alt="GMobile" class="js-lazy" data-src="//a.scdn.gr/ds/shops/logos/3870/mid_20160920155600_71ff515d.jpeg" src="//a.scdn.gr/ds/shops/logos/3870/mid_20160920155600_71ff515d.jpeg" /> 
 </noscript>

<img alt="GMobile" class="js-lazy" data-src="//a.scdn.gr/ds/shops/logos/3870/mid_20160920155600_71ff515d.jpeg" src="//c.scdn.gr/assets/transparent-325472601571f31e1bf00674c368d335.gif" />

</a>
</div>

I want to get the first img alt inside the div class shop cf and I do

  Set seller = Doc.querySelectorAll("img")      
  wks.Cells(i, "D").Value = seller.getAttribute("alt").Content(0)

I get nothing what I forget to include?!?

Can I get it from

<noscript>

tag?

I tried the following as well

  Set seller = Doc.getElementsByClassName("js-lazy")

  wks.Cells(i, "D").Value = seller.getAttribute("alt")

thank you

1 个答案:

答案 0 :(得分:0)

将元素与属性选择器一起使用

CSS:

img[alt]

VBA:

ie.document.querySelector("img[alt]")

您可能需要添加

ie.document.querySelector("img[alt]").getAttribute("alt")

要使用该类,请

ie.document.querySelector("img.js-lazy[alt]")

如果有多个元素,则使用querySelectorAll并索引到返回的nodeList中,例如

Set list = ie.document.querySelectorAll("img.js-lazy[alt]")
list.item(0).getAttribute('alt')
list.item(1).getAttribute('alt')