vb.net - WebBrowser - 从没有 ID、没有 TagName、没有 Class 的通用表中获取/读取数据
问题描述
作为标题,我正在尝试从页面中的某些表中读取数据。
我经常使用WebBrowser1.Document.GetElementById
很多类似的东西,但在这种情况下,我无法访问数据,因为在这个页面中,所有表都简单地由<table class="table">
以下是我要访问的内容定义:
<div class="col-md-6">
<div class="panel panel-primary">
<!-- Default panel contents -->
<div class="panel-heading">
<h3 class="panel-title">Ryzen 3000 - Mainstream</h3>
</div>
<!-- Table -->
<table class="table">
<tbody>
<tr>
<th>Ryzen 5 3500</th>
<td>3.6 - 4.1</td>
</tr>
<tr>
<th>Ryzen 5 3500X</th>
<td>3.6 - 4.1</td>
</tr>
<tr>
<th>Ryzen 5 3600</th>
<td>3.6 - 4.2</td>
</tr>
<tr>
<th>Ryzen 5 Pro 3600</th>
<td>3.6 - 4.2</td>
</tr>
<tr>
<th>Ryzen 5 3600X</th>
<td>3.8 - 4.4</td>
</tr>
<tr>
<th>Ryzen 5 3600XT</th>
<td>3.8 - 4.5</td>
</tr>
</tbody>
</table>
</div>
<!--</panel>-->
</div>
我在网上搜索了很多,但我找不到有效的解决方案。有人有提示吗?
解决方案
我推荐你使用 HtmlAgilityPack 和Xpath解析公式。
示例代码:
查询特定th
标签文本:
Dim doc As HtmlAgilityPack.HtmlDocument
Dim path As String = "https://www..."
Dim web As New HtmlWeb
doc = web.Load(path)
Dim node As HtmlAgilityPack.HtmlNode
'The text within the fourth <th></th>
node = doc.DocumentNode.SelectNodes("//table[@class='table']//th")(3)
Dim st As String = node.InnerText 'Ryzen 5 Pro 3600
查询所有tr
文本:
Dim doc As HtmlAgilityPack.HtmlDocument
Dim path As String = "https://www..."
Dim web As New HtmlWeb
doc = web.Load(path)
Dim nodes As HtmlAgilityPack.HtmlNodeCollection
nodes = doc.DocumentNode.SelectNodes("//table[@class='table']//tr")
For Each node As HtmlNode In nodes
RichTextBox1.AppendText(node.InnerText)
Next
推荐阅读
- react-native - React Native + Jest:为什么我需要模拟依赖项?
- .net-5 - 如何更新多对多关系中的现有对象(.Net 5)
- javascript - UPPY.js 图标部分的 HTML 元素未呈现
- python - 如何使用 cv2.Imshow 使图像大小适合屏幕
- django - 创建用户抛出 TypeError
- javascript - Jquery在数组上保存编辑的值
- swiftui - 当另一个视图从 AVCaptureDevice API 获取新值时,SwiftUI Picker View 的奇怪行为
- string - TCL 部分字符串匹配
- javascript - jQuery - 在编辑模式中捕获行字段的值
- flutter - 在 Flutter 中移除屏幕底部的空间?