0

我有一个需要从雅虎导入的表,我想将其作为宏运行。宏记录器可以很好地执行此操作,但是当我设置查询(录制时)时,我必须先登录然后选择表。

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

有没有人知道我可以如何解决这个问题,我要么将我的登录凭据放在某个输入框中,要么从用户填写的 ie 窗口传递这个 cookie?

这是我尝试导入雅虎登录页面时宏记录器返回的内容(通过网络连接窗口登录后)

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="name"> 值和下一个“

4

1 回答 1

1

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

.SavePassword = False

尝试:

.SavePassword = True

对于你的第二个问题:

有谁知道如何遍历这段代码,返回所有的 class="name"> 值和下一个

像这样的东西(需要 IE8+ 才能使用该GetElementsByClassName方法。

Dim ele as Object

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

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

Next
于 2014-08-03T16:50:33.040 回答