2015-08-15 73 views
0

我正在申请中从网页中获取数据: http://files.minecraftforge.net/maven/net/minecraftforge/forge/index_1.7.10.html阅读网站表从vb.net

在该页面中,单击“显示所有下载”按钮,就会出现一个表,我想从该表中获取数据,特别是只有“版本”列,然后将其添加到列表视图。 为此,我使用下面的代码:

Private Sub files() 
    Dim source As String = New Net.WebClient().DownloadString("http://files.minecraftforge.net/maven/net/minecraftforge/forge/index_1.7.10.html") 
    Dim recentSource As String = GetTagContents(source, "<table class=""downloadsTable"" id=""downloadsTable"">", "</table>")(0) 
    Dim lvi As New ListViewItem 
    For Each title As String In GetTagContents(recentSource, "<li>", "</li>") 
     If Not title.Contains("http:") Then 
      lvi.Text = title 
      ListView1.Items.Add(lvi) 
     End If 
    Next 
End Sub 

Private Function GetTagContents(ByVal Source As String, ByVal startTag As String, ByVal endTag As String) As List(Of String) 
    Dim StringsFound As New List(Of String) 
    Dim Index As Integer = Source.IndexOf(startTag) + startTag.Length 
    While Index <> startTag.Length - 1 
     StringsFound.Add(Source.Substring(Index, Source.IndexOf(endTag, Index) - Index)) 
     Index = Source.IndexOf(startTag, Index) + startTag.Length 
    End While 
    Return StringsFound 
End Function 

的问题是,它仅显示在表中“10.13.4.1492”的第一个值。 程序不会继续使用表中的以下行,只能保留在此处。

+0

您是否尝试使用Visual Studio的* Debug *菜单中的函数* Step Into *和* Step Over *?在函数GetTagContents()函数处放置断点(也可以在该菜单中找到)并运行该程序。然后反复使用* Step Into *并观察变量的内容 - 将鼠标悬停在它们上方以检查或将它们添加到手表中。如果你已经完成了这项研究,你可以用发现来更新你的问题。 – miroxlav

回答

1

看一下下面的代码:

Dim lvi As New ListViewItem 
For Each title As String In GetTagContents(recentSource, "<li>", "</li>") 
    If Not title.Contains("http:") Then 
     lvi.Text = title 
     ListView1.Items.Add(lvi) 
    End If 
Next 

它只创建一个ListViewItem的对象,在循环之前。你需要在每个迭代一个新的ListViewItem:

For Each title As String In GetTagContents(recentSource, "<li>", "</li>") 
    If Not title.Contains("http:") Then 
     Dim lvi As New ListViewItem 
     lvi.Text = title 
     ListView1.Items.Add(lvi) 
    End If 
Next 

甚至更​​好:

ListView1.Items.AddRange(
GetTagContents(recentSource, "<li>", "</li>"). 
    Where(Function(t) Not t.Contains("http:")). 
    Select(Function(t) New ListViewItem(t)). 
    ToArray()) 

理想情况下,你会发现这个数据的RSS提要源。 Rss是为这种刮擦而制作的。