html - 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>
我认为我的问题是程序只是找到了头盔线,而不是继续下一行。如何创建一个循环,将在每一行上找到并将前后<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
除此之外,我被困住了。
解决方案
我已经使用正则表达式解决了这个问题。
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
推荐阅读
- openlayers - 在页面中引用站点 css 时不显示 OpenLayers 2 多边形
- javascript - Knockoutjs:如何使添加到列表中的输入值也可观察
- c# - 如何将运行时参数从 LINQPad 脚本传递给运行它的 C# 类
- vba - Word Macro:根据图像比例更改页面方向
- c# - 任何方式如何根据数据库的数据行将新复选框添加到 ListView 中?
- javascript - 在 Safari 上创建具有声明属性的类不起作用
- asp.net - 使用类似应用程序的模板将单体应用程序分解为微服务
- docker - 将使用 docker 的 sh 脚本转换为 bat 脚本
- python - 无法在我的 Ubuntu 机器上安装“xlwings”
- python-3.x - Spark 中 Keras 模型的包装器