如何在vb.net 2008 app中将网页表数据(使用span类元素)抓取到listview控件中?

时间:2012-02-20 10:49:27

标签: html vb.net

我需要从网页上获取一些数据,但对我来说,主要问题是我无法以我想要的方式获取数据。我想在listview控件中获取数据,但我的代码是在listview控件的单个列中获取任何span标记内的所有数据(span类“inputFieldLabel”,这不是必需的)。 我正在给出下面网页的格式(来源)以及我在vb.net(2008)中的编码,请告诉我我在哪里做错了。

<br>
<span class="inputFieldLabel">Following Are …</span> 
<span class="inputFieldLabel">01/02/2011</span>
<span class="inputFieldLabel">And</span>
<span class="inputFieldLabel">18/02/2012</span>  
<br>   
<tr>
<td class="cTableBorder">
<span class="displayTable">1</span>
</td>
<td class=" cTableBorder ">
<form id="Agent_No_1" action="https://..." method="post">
<input type="hidden" name="Agent_No" value="490848367">
<span class="displayTable">490848367</span>
</td>
<td class="cTableBorder ">
<span class="displayTable">20000</span> 
</td>
<td class="cTableBorder ">
<span class="displayTable">564.00</span> 
</td>
<td class="cTableBorder ">
<span class="displayTable">15/02/2011</span>            
</td>

<tr>
<td class="cTableBorder">
<span class="displayTable">2</span>
</td>
<td class=" cTableBorder ">
<form id="Agent_No_1" action="https://..." method="post">
<input type="hidden" name="Agent_No" value="490842352">
<span class="displayTable">490842352</span>
</td>
<td class="cTableBorder ">
<span class="displayTable">40000</span> 
</td>
<td class="cTableBorder ">
<span class="displayTable">774.00</span> 
</td>
<td class="cTableBorder ">
<span class="displayTable">15/02/2012</span>            
</td>
</tr>

现在我的代码是

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click


Dim allelements As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("span") 
For Each DD As HtmlElement In allelements
If DD.OuterHtml.Contains("displayTable") Then
Dim z As ListViewItem
z = New ListViewItem(DD.GetAttribute("InnerText")) 
ListView1.Items.Add(z)
End If
Next
End Sub

1 个答案:

答案 0 :(得分:1)

我已经解决了我的问题。这是我的代码示例正在运行。

Dim outputstream As StreamWriter = File.CreateText("c:\listview.txt")

Dim allelements As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("td")

For Each DD As HtmlElement In allelements

If DD.GetAttribute("className") = ("cTableBorder ") Then

outputstream.WriteLine(DD.GetAttribute("InnerText"))

End If

Next

outputstream.Close()