Webページを開く必要があるExcelでタスクを自動化しようとしています。そのページのリンクに移動し、2ページ目のボタンをクリックしてダウンロードします.xlsxファイル。VBA Internet Explorerアプリケーションでは、各関数呼び出しごとに異なる結果が表示されます
これを行うスクリプトを作成しました。しかし、私がウェブページから得た回答は必ずしも同じではありません。特に、これは最初のページからダウンロードを返し、時には2番目のページに移動し、何もダウンロードしない場合があります。
これは、InternetExplorer.applicationが要求を完了するのにかかる時間と関係があります。私はスクリプトにIE.applicationがリクエストを完了するのを待つように指示しているので、これをトラブルシューティングする方法を理解することはできません。
Sub DoBrowse2()
'For Each lnk In Sheets("Sheet4").Hyperlinks
'Range(lnk).Hy.Follow
'Next
Dim i As Long
Dim URL As String
Dim BaseURL As String
Dim ToURL As String
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
Dim HWNDSrc As Long
Dim html As IHTMLDocument
Set IE = CreateObject("InternetExplorer.Application")
URL = Range("B2").Hyperlinks(1).Address
IE.Navigate URL
IE.Visible = True
Application.StatusBar = URL & " is loading. Please wait..."
Do While IE.ReadyState = 4: DoEvents: Loop
Do Until IE.ReadyState = 4: DoEvents: Loop
Application.StatusBar = URL & " Loaded"
'Set html = IE.Document
'Dim elements As IHTMLElementCollection
'Set elements = html.all
For Each itm In IE.Document.all
If itm.className = "datagrid" Then
For Each el In itm.Document.all
Debug.Print "hello"
If el.className = "ujump" And Right(el.innerText, 12) = "Constituents" Then
'Debug.Print el.innerText
ToURL = el.getAttribute("data-subset")
BaseURL = "http://datastream.thomsonreuters.com/navigator/search.aspx?dsid=ZUCH002&AppGroup=DSAddin&host=Metadata&prev=scmTELCMBR&s=D&subset="
ToURL = BaseURL & ToURL
'Debug.Print ToURL
IE.Navigate ToURL
IE.Visible = True
Do While IE.Busy
Debug.Print "in busy loop"
Application.Wait DateAdd("s", 1, Now)
Loop
GoTo end_of_for
End If
Next
End If
Next
end_of_for:
Debug.Print ("STOP STOP STOP STOP STOP")
Dim Script As String
For Each itm In IE.Document.all
If itm.className = "lgc excel" Then
Debug.Print "hello world"
Debug.Print itm.getAttribute("onclick")
itm.Click
Do While IE.Busy
Debug.Print "app busy"
Application.Wait DateAdd("s", 1, Now)
Loop
Exit For
End If
Next
End Sub
ご協力いただきありがとうございます。 IEのページが完全にロードされているかどうかを判断する
なぜブラウザで2ページ目からの要求を検討しません開発ツールを使用して、同じXHRでコンテンツを読み込んでファイルに保存しますか? – omegastripes
ご返信ありがとうございます。私はウェブ掻き取りにはあまり慣れていません。あなたはもう少し明白になりますか? – wmcass
XHR経由でファイルをダウンロードする方法を示す[ここに示した例](http://stackoverflow.com/a/32429348/2165759)より具体的な例として、最初のページのURLを教えてください。 – omegastripes