狐爸、kolen,以及其他高手,大家好,
早晨在论坛看到一个帖子,解决了以前一直想实现的需求——获取网页内容。
http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&Id=49278
但还有些疑问,恳请答疑,非常感谢。能否将代码功能及WebBrowser方法详细指点一下,非常感谢。
狐爸,
代码中很多方法在帮助文档中查询不到,是隐藏的“彩蛋”功能吗?这些功能都是很有用的功能,建议详细描述写入帮助文档,多谢啦。
1. web.Navigate
2. web.ReadyState
3. Applcation.DoEvents
4. web.Document.GetElementById("list_elem")
5. .GetElementsByTagName("tr")
--------------------------------
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