以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  [求助]模拟按钮,出现重复值(已解决)  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=161120)

--  作者:yyzlxc
--  发布时间:2021/3/6 19:14:00
--  [求助]模拟按钮,出现重复值(已解决)
下载基金的代码,出现重复值,可能是模拟按钮代码的问题,如何解决,请各位老师指教,谢谢!


 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:管理项目7.foxdb




图片点击可在新窗口打开查看此主题相关图片如下:捕获.png
图片点击可在新窗口打开查看


代码如下:

DataTables("表A").DataRows.Clear()
Dim sr As String = Format(Date.Today(), "yyyyMMdd")
Dim zr As String = Format(Date.Today(), "yyyyMMdd")
Dim web As New System.Windows.Forms.WebBrowser()
web.ScriptErrorsSuppressed = True
web.Navigate("http://fund.eastmoney.com/data/fundranking.html#tall;c0;r;s6yzf;pn50;ddesc;qsd" & sr & ";qed" & zr & ";qdii;zq;gg;gzbd;gzfs;bbzt;sfbb")
Do Until web.ReadyState = 4 AndAlso web.Document.GetElementsByTagName("table").count > 0
    Application.DoEvents
Loop
Dim trs1 As object = web.Document.GetElementById("pagebar").GetElementsByTagName("label") \'共页码
Dim n1 As Integer = trs1(6).InnerText
For n2 As Integer = 1 To n1
    Dim elems As object = web.Document.GetElementById("pagebar").GetElementsByTagName("label") \'共页码
    Dim sdt As Date = Date.Now
    While Date.Now < sdt.AddSeconds(0.1)
        Application.DoEvents
    End While
    For Each elem As object In elems
        If elem.InnerText = "下一页" Then
            elem.InvokeMember("click")
        End If
    Next
    Dim trs2 As object = web.Document.GetElementById("dbtable").GetElementsByTagName("tr") \'表
    Dim sdt1 As Date = Date.Now
    While Date.Now < sdt1.AddSeconds(0.1)
        Application.DoEvents
    End While
    For i As Integer = 1 To trs2.count - 1
        Dim tds1 = trs2(i).GetElementsByTagName("td") \'数据
        Dim tds2 = trs2(i).GetElementsByTagName("a") \'代码名称
        Dim dr As Row = Tables("表A").AddNew()
        dr("第一列") = tds2(0).InnerText \'基金代码
        dr("第二列") = tds2(1).InnerText \'基金简称
        dr("第三列") = tds1(4).InnerText \'日期
        dr("第四列") = tds1(5).InnerText \'单位净值
        dr("第五列") = tds1(6).InnerText \'累计净值
        dr("第六列") = tds1(7).InnerText \'日增长率
        dr.Save()
    Next
Next
[此贴子已经被作者于2021/3/9 22:01:22编辑过]

--  作者:有点蓝
--  发布时间:2021/3/7 20:21:00
--  
网页我就没空去分析了,我只看代码,下面应该是重复的调用

Dim trs1 As object = web.Document.GetElementById("pagebar").GetElementsByTagName("label") \'共页码
Dim n1 As Integer = trs1(6).InnerText
For n2 As Integer = 1 To n1
    Dim elems As object = web.Document.GetElementById("pagebar").GetElementsByTagName("label") \'共页码

--  作者:yyzlxc
--  发布时间:2021/3/8 19:31:00
--  
谢谢有点蓝老师的回复,1、代码中红色的第一条的目的是获取共页数,第二条是为了获取“下一页”的按钮,这两条代码如何合并,请老师指教。
                                2、出现数据重复,是不是因为“下一页”按钮模拟失效,如何判断“下一页”按钮有效,还请老师指教,谢谢!!

--  作者:有点蓝
--  发布时间:2021/3/8 20:27:00
--  
没有必要获取总共多少页,逻辑应该是处理当前页,然后再点击下一页,等页面刷新,在处理当前页,然后继续再点击下一页......
--  作者:yyzlxc
--  发布时间:2021/3/9 22:01:00
--  
谢谢有点蓝老师的指教,修改代码后,问题解决,代码如下:

DataTables("表A").DataRows.Clear()
Dim sr As String = Format(Date.Today(), "yyyyMMdd")
Dim zr As String = Format(Date.Today(), "yyyyMMdd")
Dim web As New System.Windows.Forms.WebBrowser()
web.ScriptErrorsSuppressed = True
web.Navigate("http://fund.eastmoney.com/data/fundranking.html#tall;c0;r;s6yzf;pn50;ddesc;qsd" & sr & ";qed" & zr & ";qdii;zq;gg;gzbd;gzfs;bbzt;sfbb")
Do Until web.ReadyState = 4 AndAlso web.Document.GetElementsByTagName("table").count > 0
    Application.DoEvents
Loop
Dim trs1 As object = web.Document.GetElementById("pagebar").GetElementsByTagName("label") \'页码
Dim n1 As Integer = trs1(6).InnerText ‘提取总页码数
Dim Innertext = ""
Dim n2 As Integer = 1
Do \'页循环
    innertext = web.Document.GetElementById("dbtable").Innertext
    Dim trs2 As object = web.Document.GetElementById("dbtable").GetElementsByTagName("tr") \'表
    For i As Integer = 1 To trs2.count - 1
        Dim tds1 = trs2(i).GetElementsByTagName("td") \'数据
        Dim dr As Row = Tables("表A").AddNew()
        dr("第一列") = tds1(1).InnerText \'序号
        dr("第二列") = tds1(2).InnerText \'基金代码
        dr("第三列") = tds1(3).InnerText \'基金简称
        dr.Save()
    Next
    Dim elems As object = web.Document.GetElementById("pagebar").GetElementsByTagName("label") \'提取按钮
    For Each elem As object In elems
        If elem.InnerText = "下一页" Then
            elem.InvokeMember("click")
        End If
    Next
    Do Until web.ReadyState = 4 AndAlso web.Document.GetElementById("dbtable").Innertext <> Innertext \'判断不重复
        Application.DoEvents
    Loop
    n2 += 1
Loop While n2 <= n1 \'页数据