マクロとして実行したい yahoo からインポートする必要があるテーブルがあります。マクロ レコーダーはこれをうまく処理しますが、クエリを設定するとき (記録するとき) は、最初にログインしてからテーブルを選択する必要があります。
Excel を閉じない限り、クエリはマクロ内で機能します。Excel を再起動すると、Cookie が失われたようで、クエリ インターフェイス経由で yahoo にログインしないと機能しません。新しい IE ウィンドウを開いてログインしようとしましたが、Cookie がクエリに渡されないようです。
ログイン資格情報を入力ボックスに入力するか、ユーザーが入力する ie ウィンドウからこの Cookie を渡す方法について、これを回避する方法を知っている人はいますか?
yahooログインページをインポートしようとしたときにマクロレコーダーが返すものは次のとおりです(Web接続ウィンドウからサインインした後)
Sub Macro1()
With ActiveSheet.QueryTables.Add(Connection:="URL;https://login.yahoo.com", _
Destination:=Range("$A$1"))
.CommandType = 0
.Name = "login.yahoo"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlEntirePage
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = True
.Refresh BackgroundQuery:=False
End With
End Sub
編集:私はこれを別の方法で行おうとしています。だから私は次の方法でyahooにログインしました:
Sub gotosite()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "http://login.yahoo.com"
'IE.Visible = True
'apiShowWindow IE.hwnd, SW_MAXIMIZE
Do While IE.Busy: DoEvents: Loop
Do While IE.ReadyState <> 4: DoEvents: Loop
IE.Document.all("login").Value = "username"
IE.Document.all("passwd").Value = "password"
IE.Document.all(".save").Click
Do While IE.Busy: DoEvents: Loop
Do While IE.ReadyState <> 4: DoEvents: Loop
IE.Navigate "http://football.fantasysports.yahoo.com/archive/nfl/2013/715896/draftresults"
'IE.Visible = True
'apiShowWindow IE.hwnd, SW_MAXIMIZE
Do While IE.Busy: DoEvents: Loop
Do While IE.ReadyState <> 4: DoEvents: Loop
これにより、スクレイピングしようとしているサイトにページが表示されます。対象のソース コードは次のようになります。
<div id="drafttables" class="round">
<div class="yui-gb">
<div class="yui-u first">
<table cellpadding="0" cellspacing="0" border="0" class="simpletable">
<thead>
<tr>
<th colspan="3">Round 1</th>
</tr>
</thead>
<tbody>
<tr class="odd"> <td class="first">1.</td>
<td class="player" nowrap><a href="http://sports.yahoo.com/nfl/players/8261" target="sports" class="name">Adrian Peterson</a> <span>(Min - RB)</span></td>
<td class="last" title="Tale of the Gerbil">Tale of the ...</td>
</tr>
<tr class="even"> <td class="first">2.</td>
<td class="player" nowrap><a href="http://sports.yahoo.com/nfl/players/25741" target="sports" class="name">Doug Martin</a> <span>(TB - RB)</span></td>
<td class="last" title="Michael Korte's Team">Michael Kort...</td>
</tr>
<tr class="odd"> <td class="first">3.</td>
このコードをループして、すべての class="name"> 値と次の "