【发布时间】:2019-11-28 14:03:10
【问题描述】:
我找到了一个将 web 表解析为 csv 并将数据写入计算机中的文本文件的代码示例,但我想将其解析为我的 datagridview,而不是计算机位置。
我不明白为什么我不能像上面所说的那样添加这个问题:看起来 就像您的帖子主要是代码一样;请添加更多细节;这不是好的Stuckoverflow!我不需要太多说话的 ricky 我只是有几个单词和代码示例这就是这个网站应该是关于而不是弹出警告'写更多人'哈哈。
这里是html网站:
<table id="example" class="display" style="width:100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr>
<td>Jennifer Chang</td>
<td>Regional Director</td>
<td>Singapore</td>
<td>28</td>
<td>2010/11/14</td>
<td>$357,650</td>
</tr>
<tr>
<td>Brenden Wagner</td>
<td>Software Engineer</td>
<td>San Francisco</td>
<td>28</td>
<td>2011/06/07</td>
<td>$206,850</td>
</tr>
<tr>
<td>Sakura Yamamoto</td>
<td>Support Engineer</td>
<td>Tokyo</td>
<td>37</td>
<td>2009/08/19</td>
<td>$139,575</td>
</tr>
<tr>
<td>Donna Snider</td>
<td>Customer Support</td>
<td>New York</td>
<td>27</td>
<td>2011/01/25</td>
<td>$112,000</td>
</tr>
</tbody>
<tfoot>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</tfoot>
</table>
这是需要修改的代码:
Imports System.IO
Imports System.Net
Imports HtmlAgilityPack
Public Class Class1
Public Function Demo1() As DataTable
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
Dim Document As New HtmlAgilityPack.HtmlDocument
Dim myHttpWebRequest = CType(WebRequest.Create("https://website.com/table.html"), HttpWebRequest)
myHttpWebRequest.UserAgent = "Mozilla/5.0 (compat ble; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)"
Dim streamRead = New StreamReader(CType(myHttpWebRequest.GetResponse(), HttpWebResponse).GetResponseStream)
Dim res As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
Document.Load(res.GetResponseStream, True)
Dim table As HtmlAgilityPack.HtmlNode = Document.DocumentNode.SelectSingleNode("//table[@id='example']")
Dim dt As New DataTable()
If table IsNot Nothing Then
Dim rows = table.SelectNodes("tr")
If rows Is Nothing AndAlso table.SelectSingleNode("tbody") IsNot Nothing Then
rows = table.SelectSingleNode("tbody").SelectNodes("tr")
End If
For row As Integer = 0 To rows.Count - 1
'if row = then these are headers
If row = 0 Then
Dim cols = rows(row).SelectNodes("th")
dt.Columns.Add(New DataColumn(cols(0).InnerText.ToString()))
dt.Columns.Add(New DataColumn(cols(1).InnerText.ToString()))
dt.Columns.Add(New DataColumn(cols(2).InnerText.ToString()))
dt.Columns.Add(New DataColumn(cols(3).InnerText.ToString()))
dt.Columns.Add(New DataColumn(cols(4).InnerText.ToString()))
dt.Columns.Add(New DataColumn(cols(5).InnerText.ToString()))
Else
Dim cols = rows(row).SelectNodes("td")
Dim dr As DataRow = dt.NewRow()
dr(0) = cols(0).InnerText.ToString()
dr(1) = cols(1).InnerText.ToString()
dr(2) = cols(2).InnerText.ToString()
dr(3) = cols(3).InnerText.ToString()
dr(4) = cols(4).InnerText.ToString()
dr(5) = cols(5).InnerText.ToString()
dt.Rows.Add(dr)
End If
Next
End If
Return dt
End Function
End Class
感谢您的帮助。
【问题讨论】:
-
警告您的帖子主要是代码的原因是您需要解释您的问题。恐怕只是发布代码并要求某人修复它不是这里的主题。
标签: vb.net parsing datagridview html-agility-pack