通过宏中的查询登录到Yahoo。饼干传递?

时间:2014-08-03 13:40:30

标签: vba excel-web-query

我有一个表,我需要从雅虎导入,我想作为宏运行。 Macro Recorder做得很好,但是当我设置查询时(录制时)我必须首先登录然后选择表格。

只要我不关闭excel,查询就可以在宏中运行。当我重新进入excel时,似乎cookie丢失了,除非我通过查询界面登录yahoo,否则它不起作用。我尝试打开一个新的IE窗口并通过那里登录,但似乎cookie没有传递给查询。

有没有人知道如何解决这个问题,我将登录凭据放在某个输入框中,或者从用户填写的窗口传递此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

编辑:我试图以不同的方式解决这个问题。所以我通过以下方法登录雅虎:

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 =&#34; name&#34;&gt;价值和下一个&#34;

1 个答案:

答案 0 :(得分:1)

在您的第一次尝试中(使用QueryTables,这是从网络上抓取数据的一种非常不可靠的方法)您是否只是想让它记住密码?现在,你有:

.SavePassword = False

尝试:

.SavePassword = True

关于你的第二个问题:

  

有没有人知道如何遍历此代码,返回所有class =&#34; name&#34;&gt;值和下一个

像这样的东西(要求IE8 +使用GetElementsByClassName方法。

Dim ele as Object

For each ele in IE.Document.GetElementsByClassName("name")

    Debug.Print ele.Value ' or ele.InnerText, etc...

Next