首页 > 解决方案 > Visual Basic 读取 HTML 以使用循环添加到列表

问题描述

我正在制作一个从该网站的HTML 中获取数据的程序。这个网站每个月都会更新它的列表,有时会有不同的项目到目前为止,我能够选择 3 个不同的值,即头盔、粘性手榴弹和 C4

但是,当我尝试获取 Armored Vest 值时,我得到了另一个头盔。这是我的源代码

Imports System.IO
Imports System.Net
Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim pos1 As Long, pos2 As Long, newitem As String, findstring As String
        Dim request As WebRequest = WebRequest.Create("http://cosmic-octane-202719.appspot.com/")
        Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
        Dim datastream As Stream = response.GetResponseStream
        Dim reader As New StreamReader(datastream)
        Dim strData As String = reader.ReadToEnd

        'Keep getting 7 Helmets
        For currentitem As Integer = 1 To 7
            findstring = "<li><strong"
            pos1 = InStr(strData, findstring)
            pos1 = InStr(pos1 + 1, strData, ">", vbTextCompare)
            pos2 = InStr(pos1 + 1, strData, "</strong></li>", vbTextCompare)

            newitem = strData.Substring(pos1 + findstring.Length - 3, pos2 - pos1 - findstring.Length + 2)
            ListBox1.Items.Add(newitem)
        Next

        'Sticky Grenade
        findstring = "<li>Lethal: <strong"
        pos1 = InStr(strData, findstring)
        pos1 = InStr(pos1 + 1, strData, ">", vbTextCompare)
        pos2 = InStr(pos1 + 1, strData, "</strong></li>", vbTextCompare)

        newitem = strData.Substring(pos1 + findstring.Length - 3, pos2 - pos1 - findstring.Length + 2)
        ListBox1.Items.Add(newitem)

        'C4
        findstring = "<li>Placeable: <strong"
        pos1 = InStr(strData, findstring)
        pos1 = InStr(pos1 + 1, strData, ">", vbTextCompare)
        pos2 = InStr(pos1 + 1, strData, "</strong></li>", vbTextCompare)

        newitem = strData.Substring(pos1 + findstring.Length - 3, pos2 - pos1 - findstring.Length + 2)
        ListBox1.Items.Add(newitem)
    End Sub
End Class

HTML 片段是

<ul class="disc">
  <li><strong>Helmet</strong></li>
  <li><strong>Armored Vest</strong></li>
  <li><strong>Mk. 42</strong> <span class="red">(Reflex + Grip)</span></li>
  <li><strong>Uzi</strong> <span class="red">(Rapid Fire + Extended Clip)</span></li>
  <li><strong>Combat Knife</strong> <span class="red">(Grip)</span></li>
  <li><strong>Fast Reload</strong> <span class="red">(Advanced)</span></li>
  <li><strong>FMJ</strong> <span class="red">(Basic)</span></li>
  <li>Lethal: <strong>Sticky Grenade</strong></li>
  <li>Placeable: <strong>C4</strong></li>
</ul>
结果: 7个头盔

我认为我的问题是程序只是找到了头盔线,而不是继续下一行。如何创建一个循环,将在每一行上找到并将前后<li><strong>的文本添加到列表框中。例如<li><strong></strong>

<li><strong>Helmet</strong></li> would put Helmet into listbox1.
<li><strong>Armored Vest</strong></li> would put Armored Vest into listbox1.
<li><strong>Mk. 42</strong> <span class="red">(Reflex + Grip)</span></li> would put Mk. 42 into listbox1

除此之外,我被困住了。

标签: htmlvb.netloopslistboxfind

解决方案


我已经使用正则表达式解决了这个问题。

Dim items As MatchCollection = Regex.Matches(strData, "(?<=<li><strong>).*(?=<\/strong)")
For Each match As Match In items
    For Each item As Capture In match.Captures
        ListBox1.Items.Add(item.Value)
    Next
Next

完整的代码是

Imports System.IO
Imports System.Net
Imports System.Text.RegularExpressions
Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim pos1 As Long, pos2 As Long, newitem As String, findstring As String
        Dim request As WebRequest = WebRequest.Create("http://cosmic-octane-202719.appspot.com/")
        Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
        Dim datastream As Stream = response.GetResponseStream
        Dim reader As New StreamReader(datastream)
        Dim strData As String = reader.ReadToEnd
        Dim items As MatchCollection = Regex.Matches(strData, "(?<=<li><strong>).*(?=<\/strong)")
        For Each match As Match In items
            For Each item As Capture In match.Captures
                ListBox1.Items.Add(item.Value)
            Next
        Next
        findstring = "<li>Lethal: <strong"
        pos1 = InStr(strData, findstring)
        pos1 = InStr(pos1 + 1, strData, ">", vbTextCompare)
        pos2 = InStr(pos1 + 1, strData, "</strong></li>", vbTextCompare)
        ListBox1.Items.Add(strData.Substring(pos1 + findstring.Length - 3, pos2 - pos1 - findstring.Length + 2))
        findstring = "<li>Placeable: <strong"
        pos1 = InStr(strData, findstring)
        pos1 = InStr(pos1 + 1, strData, ">", vbTextCompare)
        pos2 = InStr(pos1 + 1, strData, "</strong></li>", vbTextCompare)
        ListBox1.Items.Add(strData.Substring(pos1 + findstring.Length - 3, pos2 - pos1 - findstring.Length + 2))
    End Sub
End Class

结果


推荐阅读