1

首先,我对编程比较陌生。我一直在让自己主要熟悉 VBA。我以前在这里问过问题,到目前为止,这些回答非常有帮助,所以我再次试试运气。

我要去这个网站:http ://www.otcmarkets.com/stock-screener

我希望我的脚本可以这样说:

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

我还希望它循环并单击此 html 后面的下一个按钮,直到完成:a href="javascript:void(null);">next & 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

对不起意大利面条代码,就像我说我是新人一样

4

1 回答 1

1

我不太清楚你想要得到什么(为什么不使用页面顶部方便的“全部下载”链接?)

已编辑- 将单元格值放在工作表 EDIT2上- 单击“下一步”链接。 EDIT3 - 循环

这种事情是一个彻头彻尾的兔子洞。

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
于 2013-05-21T05:07:55.613 回答