以文本方式查看主题 - Foxtable(狐表) (http://foxtable.com/bbs/index.asp) -- 专家坐堂 (http://foxtable.com/bbs/list.asp?boardid=2) ---- 想从网页上提取贵金属数据后直接进行预算成本核算 (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=173578) |
||||
-- 作者:zhuxinhui -- 发布时间:2021/12/6 16:46:00 -- 想从网页上提取贵金属数据后直接进行预算成本核算 下面这些代码的知识在哪些章节能找到,我用精灵都查不出来的,想搞清楚后再修改下,但查不到 Dim web As New System.Windows.Forms.WebBrowser() web.Navigate("http://www.ccmn.cn/historyprice/cjxh_1/") Do Until web.ReadyState = 4 Application.DoEvents Loop \'取数据 Dim Cols() As String = { "序号","品名","价格区间","均价","涨跌","单位","发布日期" } Dim elems As object = web.Document.GetElementById("list_elem").GetElementsByTagName("tr") Dim m As Integer = 0 For Each elem As object In elems Dim i As Integer = -1 m = m + 1 If m > 1 Then \'因为网页的表格有标题,如果不判断,就会增加出一个空行 Tables("长江现货").AddNew End If Dim tdelems As object = elem.GetElementsByTagName("td") For Each tdelem As object In tdelems i = i + 1 Tables("长江现货").Current(Cols(i)) = tdelem.Innertext Tables("长江现货").Current("录入日期") = Date.Today If i > 2 Then \'必须要当价格区间列已经有值,才能执行以下代码 Dim s As String = Tables("长江现货").Current("价格区间") Dim s2 As String = s.SubString(5,1) \'价格区间中间的横杠有点特殊,不是键盘上的短横杠.所以用Asc()函数和Chr(-24150))配合来引用. Dim k As String = s.IndexOf(Chr(-24150)) \'用Asc(s2)函数获得-的字符编码是-24150 Tables("长江现货").Current("最高价") = s.SubString(k+1) Tables("长江现货").Current("最低价") = s.Remove(k) End If Next Next Tables("长江现货").save
|
||||
-- 作者:有点蓝 -- 发布时间:2021/12/6 17:06:00 -- 帮助没有这些用法,需要到微软网站查.net的使用帮助:https://docs.microsoft.com/zh-cn/dotnet/api/system.windows.forms.htmldocument?view=netframework-4.0 |
||||
-- 作者:zhuxinhui -- 发布时间:2021/12/6 17:32:00 -- 上面的代码都是在网页的页面源代码中提取数据的吗? |
||||
-- 作者:zhuxinhui -- 发布时间:2021/12/7 15:06:00 -- 此主题相关图片如下:3.png Dim web As New System.Windows.Forms.WebBrowser() msgbox(0) web.Navigate("https://www.ccmn.cn/cjysw.shtml") msgbox(0.1) Do Until web.ReadyState = 4 Application.DoEvents Loop msgbox(1) Dim Cols() As String = { "序号","品名","最低价","最高价","均价","涨跌","日期" } Dim elems As object = web.Document.GetElementById("table1").GetElementsByTagName("tr") msgbox(2) For Each elem As object In elems Dim tds As object = elem.GetElementsByTagName("td") msgbox(3) If tds.Count > 0 AndAlso tds.Count < 7 msgbox(tds.Count) msgbox(tds(0).innerText & ":" & tds(1).innerText & ",," & tds(2).innerText & ":" & tds(3).innerText & ",," & tds(4).innerText & ":" & tds(5).innerText & ":" & tds(6).innerText) Dim ndr = Tables("金属报价").AddNew ndr("序号") = tds(0).innerText ndr("品名") = tds(1).innerText ndr("最低价") = tds(2).innerText ndr("最高价") = tds(3).innerText ndr("均价") = tds(4).innerText ndr("涨跌") = tds(5).innerText ndr("日期") = tds(6).innerText End If Next Tables("金属报价").save
|
||||
-- 作者:zhuxinhui -- 发布时间:2021/12/7 15:06:00 --
|
||||
-- 作者:zhuxinhui -- 发布时间:2021/12/7 15:11:00 -- 这个应该是ID吧 此主题相关图片如下:5.png |
||||
-- 作者:有点蓝 -- 发布时间:2021/12/7 15:29:00 -- 有id=xxx这种的就是id。.net的浏览器控件比较老了,不支持很多新特性,所以会出错。 |
||||
-- 作者:zhuxinhui -- 发布时间:2021/12/7 15:36:00 -- 甘要怎样搞 |
||||
-- 作者:有点蓝 -- 发布时间:2021/12/7 15:47:00 -- 屏蔽掉错误 Dim web As New System.Windows.Forms.WebBrowser() web.ScriptErrorsSuppressed = True
|
||||
-- 作者:zhuxinhui -- 发布时间:2021/12/7 15:49:00 -- 没有增加到行 |