html - Get text from website with VBA -
i need create vba macro takes specific website , search id. when id found need take text , copy excel.
here source code of webiste:
<tr> <td style="width: 10%; color: blue" valign="top"><a name="111" id="111">111</td> <td><pre> text need in excel </pre></a><td> </tr>
i need text between "pre"
this try in vba:
set ie = createobject("internetexplorer.application") ie.visible = true ie.navigate website_url while ie.busy , not ie.readystate = readystate_complete doevents loop set document = ie.document dim searchvalue string set element = document.getelementbyid(searchvalue).getattribute("pre") range("i1").select activecell.formular1c1 = element
i tried instead of ".getattribute" other methods , tried use element string didn't work.
it awesome if me code :d
the text not located in attribute in pre
element. getattribute
function can't return desired text.
have @ function queryselector
if want first text. function returns ihtmlelement
, accepts selector.
if wish return texts try function queryselectorall
. function returns ihtmldomchildrencollection
and accepts selector well. hth
example:
' add reference microsoft internet controls (shdocvw) ' add reference microsoft html object library dim selector string ' select element id = searchvalue has td has pre selector = "#" & searchvalue & " td pre" dim onepre ihtmlelement set onepre = doc.queryselector(selector) if not onepre nothing msgbox "first pre element text: " & onepre.innertext end if dim allpre ihtmldomchildrencollection set allpre = doc.queryselectorall(selector) if allpre.length > 0 dim el, text el = 0 allpre.length - 1 text = text & allpre.item(el).innertext next msgbox "all pre elements text: " & text end if ie.quit
Comments
Post a Comment