excel - VBA script to pull values within specific HTML classes -


i created vba script pull prices websites getting value within html class.

please see vba script pull data website more context.

this works there cases there 1 price (no rrp & sale price) , therefore need somehow incorporate if statement class name, if doesn't exist another.

for example have following spreadsheet:

| | b | c | | | item | price | | | bfd/garden-structures/arbours/arbours-sunflower | | | | bfd/garden-structures/arbours/tatton-corner-arbour-seat | | | | bsd/garden-storage/wooden-storage/4-x-2-windsor-garden-storage-chest | | 

in example first 2 work code below: (looking int class variantprice & nowvalue) 3rd example doesn't work classes variantprice & nowvalue not exist, have class price & singleprice.

the code have used below:

sub buydeckingdirect()      dim ie new internetexplorer     dim doc htmldocument     dim result ihtmlelement     dim result2 ihtmlelement     dim item string     dim lrow long      'ie.visible = true'     lrow = 2     item = worksheets("buydeckingdirect").range("b" & lrow).value     msgbox "price dump started"     until item = ""         ie.navigate "http://www.buydeckingdirect.co.uk/" & item                      doevents         loop until ie.readystate = readystate_complete          set doc = ie.document          set result = doc.queryselector(".variantprice")         set result2 = result.queryselector(".nowvalue")          worksheets("buydeckingdirect").range("c" & lrow).value = result2.innertext          lrow = lrow + 1         item = worksheets("buydeckingdirect").range("b" & lrow).value      loop     msgbox "buydeckingdirect price dump complete" end sub 

any appreciated!

thanks

jess

combine both classes in call queryselector , if result nothing, call again alternative class.

example:

' ... set result = doc.queryselector(".variantprice .nowvalue") if result nothing     set result = doc.queryselector(".variantprice .price") end if ' should consider fact can have neither if result nothing     worksheets(...etc...).value = "n/a" else     worksheets(...etc...).value = result.innertext end if 

of course can check existence of nowvalue class after setting result so:

' ... set result = doc.queryselector(".variantprice") if isnull(result.queryselector(".nowvalue"))     set result2 = result.queryselector(".price") else     set result2 = result.queryselector(".nowvalue") end if 

personally, prefer 1st option it's use case.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -