此文仅为抛砖引玉之用
在VBA中添加引用Microsoft HTML Object Library之后,可以使用html对象,作为无头浏览器,让它加载页面,让它执行javascript脚本,用它输出数据等
复制了网页HTML代码,可以粘贴到excel作为表格
用power query的advanced editor即m 语言,也可以从网页加载,亦即Web.Content及Web.Page函数,也可以指定HTML及javascript脚本文本。
VBA中的操作
方法一,XML库下载
引用:
Microsoft HTML Object Library
Microsoft XML, v6.0
'=================== GET js data from nowgoal =======================Dim http As New XMLHTTP60, res As VariantDim s As StringWith http.Open "GET", "http://info.nowgoal.com/jsData/matchResult/" & season & "/s" & leagueID & "_en.js", False.sendres = .responseTextEnd With'=============== generate javascript, to render json =================Dim HTML As IHTMLDocument2Dim w As HTMLWindow2Set HTML = New HTMLDocumentSet w = HTML.parentWindowtt = "s="""";var teamd={};" & _"for (i_team=0;i_team<arrTeam.length;i_team++){teamd[arrTeam[i_team][0]]=arrTeam[i_team][3];}" & _"for (round in jh){" & _"for (matches in jh[round]){" & _"s+=""<tr>"";" & _"s+=""<td>""+round.substring(2)+""</td>"";" & _"for (i_match=0;i_match<jh[round][matches].length;i_match++) {" & _"s1=jh[round][matches][i_match];" & _"if (i_match==4 || i_match==5){" & _"s+=""<td>""+teamd[s1]+""</td>"";" & _"}else{s+=""<td>""+s1+""</td>"";}}" & _"s+=""</tr>"";}}"While InStr(res, ",,") > 0res = Replace(res, ",,", ",'',")Wendres = Replace(res, "'", """")'====================== execute javascript ===========================w.execScript "var jh={};"w.execScript resw.execScript (tt)'=================== render json to HTML Table =======================HTML.body.innerHTML = "<html><head></head><body><table id=""jht"">" & w.s & "</table></body></html>"'========================= copy and paste ============================w.execScript ("var ctrlRange = document.body.createControlRange();ctrlRange.add(document.getElementById('jht'));ctrlRange.execCommand(""Copy"");")
方法二,使用Internet Controls下载
引用:
Microsoft HTML Object Library
Microsoft Internet Controls
Dim elslds As ObjectDim html As New HTMLDocumentSet Soccerwayhtml = CreateObject("Msxml2.ServerXMLHTTP")Url="https://int.soccerway.com/matches/2022/07/04/united-states/mls/nashville-mls/portland-timbers-mls/3709463/"With Soccerwayhtml.Open "GET", Url, False.setRequestHeader "Accept", "text/html".setRequestHeader "Accept-Language", "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7".setRequestHeader "Accept-Encoding", "identity".setRequestHeader "Cookie", "___ws_d_st={}; sw_l10m=us; sw_l10org=US".setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36".sendresdata = .responseTextEnd WithDo Until Soccerwayhtml.readyState = 4DoEventsLoop'===处理数据===html.body.innerHTML = resdataSet elslds = html.getElementsByClassName("sidelined-content")
