VB2017:我循环浏览网页中的各个部分,并将多个表解析为HTML节点。这需要花费一些时间,因此我正在运行从源到HtmlNodeCollection的异步转换。我得到了结果,并且已经确认它们是我所需要的。
但是,我对如何将每个Task的结果的占位符分配给类字段感到有些困惑。接下来,每个Section类应将每个任务的结果保存在Section.SectionNodes中。分配Task.Result是一个阻止调用,所以这不是我所需要的。我想我正在寻找占位符或最终结果的地址。这种方法可行吗?
Private Sub GetHtmlNodes()
'driver = Selenium web driver
Dim taskList = New List(Of Task(Of HtmlAgilityPack.HtmlNodeCollection))
For idxSec As Integer = 0 To 5
Dim currSec As String = mySecs(idxSec)
'create a section and get all nodes in the section. this is async so we will have to account for that later on after the loop.
Dim sec As New Section
sec.SectionId = currSec
Dim tsk As Task(Of HtmlAgilityPack.HtmlNodeCollection) = EnumSectionNodesAsync(driver.PageSource, currSec)
taskList.Add(tsk)
'sec.SectionNodes = tsk.Result 'this is a block and will run the task sync
Next idxSec
'wait untill all tasks are complete
Task.WaitAll(taskList.ToArray)
For Each tsk As Task(Of HtmlAgilityPack.HtmlNodeCollection) In taskList
Debug.Print("Task HtmlNodeCollection node count={0} ", tsk.Result.Count)
Next tsk
End Sub
Private Function EnumSectionNodesAsync(ByVal htmlSource As String, ByVal currSec As String) As Task(Of HtmlAgilityPack.HtmlNodeCollection)
'get all the nodes in the current section
Dim xpath As String = "my xpath to the HTML section"
Dim htmlDocument As New HtmlAgilityPack.HtmlDocument
htmlDocument.LoadHtml(htmlSource)
Dim tsk As Task(Of HtmlAgilityPack.HtmlNodeCollection) = Task.Run(Function() htmlDocument.DocumentNode.SelectNodes(xpath))
Return tsk
End Function