2013-05-21 36 views
1

首先,我對編程比較陌生。我一直主要熟悉VBA。我之前在這裏提過問題,到目前爲止,答覆非常有幫助,所以我再次嘗試我的運氣。if getelementby class

我要去這個網站:http://www.otcmarkets.com/stock-screener

我想我的腳本是這樣說的:

if ElementClassName("listingOdd").innertext = "USA, NV" 
then GetElementsbyClassName("listingOddlistingOdd whiteSpaceNormal").innertext 

我也想它遍歷並單擊下一步按鈕,這是這個網站的背後直到它完成: a href =「javascript:void(null);」>下一個& gt;/a

歡迎任何幫助,我一直在搜索過去的問題和看教程,不要問我是否在這個問題上沒有認真地敲我的頭。非常感謝提前

這是一些代碼,我都試過了,並出現以下錯誤:「對象犯規支持此方法」 有從試驗和錯誤

Sub t() 


Dim IE As Object 
Set IE = CreateObject("InternetExplorer.Application") 
IE.Visible = False 

IE.navigate "http://www.otcmarkets.com/stock-screener" 

Do 
DoEvents 
Loop Until IE.readyState = READYSTATE_COMPLETE 
Dim Doc As HTMLDocument 
Set Doc = IE.document 
Dim sDD As String 
Dim lnk As Variant 
sDD = Trim(Doc.GetElementbyClassName("listingOddlistingOdd whiteSpaceNormal").innerText) 
'If lnk.classame.innerText = "USA, NV" Then 
If lnk.innerText = "USA, NV" Then 
'If ElementClassName("listingOdd")(0).innerText = "USA, NV" Then 
'sDD 
'sDD = Trim(Doc.GetElementbyClassName("listingOddlistingOdd whiteSpaceNormal").innerText) 
MsgBox sDD 
End If 




End Sub 

對不起註釋掉了很多意大利麪代碼,就像我說的我是新的

+0

'getElementsByClassName'發表一些實際的代碼,你試過,和國家有什麼錯誤(S)你... –

+0

更新蒂姆·威廉姆斯 –

回答

1

我並不十分清楚你想要得到什麼(爲什麼不只是使用方便的「下載全部」鏈接在頁面頂部?)

編輯 - 在工作表上放置單元格值 EDIT2 - 單擊「下一個」鏈接。 編輯3 - 環

雖然這種事情是一個總兔子洞。

Sub Tester() 
Dim doc As Object 
Dim IE As Object, nxt 
Dim rng As Range, x As Integer 

    Set rng = ThisWorkbook.Sheets("sheet1").Range("A2") 
    Set IE = CreateObject("InternetExplorer.Application") 
    IE.Visible = True 
    IE.navigate "http://www.otcmarkets.com/stock-screener" 
    WaitForLoad IE 

    Set nxt = GetNext(IE) 
    Do While Not nxt Is Nothing 
     ExtractTableContent IE, rng 
     nxt.Click 
     WaitForLoad IE 
     Set nxt = GetNext(IE) 
    Loop 
End Sub 

Sub WaitForLoad(IE As Object) 
    Do: DoEvents: Loop Until IE.readyState = 4 'READYSTATE_COMPLETE 
    Application.Wait Now + TimeSerial(0, 0, 3) 
End Sub 
'copy table content 
Sub ExtractTableContent(ByRef IE As Object, ByRef rng As Range) 
    Dim tableDiv As Object, r As Object, rw As Object, x As Long 

    Set tableDiv = IE.document.getelementbyid("stockScreenerResults") 
    Set r = tableDiv.getelementsbytagname("table")(0).Rows 
    For Each rw In r 
     For x = 1 To rw.Cells.Length 
      rng.Offset(0, x - 1).Value = rw.Cells(x - 1).innerText 
     Next x 
     Set rng = rng.Offset(1, 0) 
    Next rw 
End Sub 

'find the link which takes you to the next page... 
Function GetNext(IE As Object) As Object 
    Dim links, l As Object, rv As Object, cn As Object 
    Set links = IE.document.getelementsbytagname("a") 
    For Each l In links 
     If l.innerText Like "*next*" Then 
     Set rv = l 
     Exit For 
     End If 
    Next l 
    Set GetNext = rv 
End Function 
+0

所提供的下載鏈接的問題是,它並沒有給我只能將結果限制在內華達州,這給我提供了不完整的結果,因爲它將我限制在2000個搜索結果中。我確實先嚐試過。感謝您的耐心和時間回答。我給你信用=)再次感謝 –

+0

冒着無知的風險,我沒有得到任何結果打印到我的電子表格中的單元格 –

+0

'Debug.Print'打印在調試窗口中,而不是在電子表格中。 –

相關問題