我的程序旨在搜索亚马逊并从结果中提取数据 - 例如产品名称和价格。
到目前为止,我的程序会将用户键入的内容粘贴到txt_Search
并按下页面上的“Go”。这是迄今为止的代码:
Private Sub btn_Search_Click(sender As Object, e As EventArgs) Handles btn_Search.Click
Dim SearchTerm As String
'Set SearchTerm as text input
SearchTerm = txt_Search.Text
'Identify the search text box
Dim AmazonElementCollection As HtmlElementCollection = web_Browser.Document.GetElementsByTagName("Input")
For Each curElement As HtmlElement In AmazonElementCollection
Dim searchtextbox As String = curElement.GetAttribute("id").ToString
If searchtextbox = "twotabsearchtextbox" Then
'Populate the search text box
curElement.SetAttribute("Value", searchterm)
End If
Next
'BUTTON CLICK: Search button
'<input type="submit" value="Go" class="nav-submit-input" title="Go">
For Each curElement As HtmlElement In AmazonElementCollection
'Find the Go button
If curElement.GetAttribute("value").Equals("Go") Then
'Function to click the button
curElement.InvokeMember("click")
End If
Next
所以这一切都运行正常,因为它在名为web_Browser
的Web浏览器对象上执行了所有操作。但是,如何从文本中取出第一个结果的文本,并将其放入文本框中。我希望将第一个结果的标题放入名为txt_Title
的文本框中。谢谢。
答案 0 :(得分:0)
您需要为web_browser控件的DocumentCompleted事件添加事件处理程序。
在该事件处理程序中,您可以使用相同的方法查找结果元素,就像查找搜索文本框和搜索按钮一样,即:遍历文档的元素并使用属性用于识别您想要的元素的元素。
这看起来像这样:
添加事件处理程序:
If curElement.GetAttribute("value").Equals("Go") Then
'Add the handler
AddHandler web_Browser.DocumentCompleted, AddressOf myDocumentCompleted
'Function to click the button
curElement.InvokeMember("click")
End If
事件处理程序实现:
Private Sub myDocumentCompleted(ByVal sender As Object, _
ByVal e As WebBrowserDocumentCompletedEventArgs)
Dim AmazonElementCollection As HtmlElementCollection = web_Browser.Document.GetElementsByTagName("something you need to figure out")
For Each curElement As HtmlElement In AmazonElementCollection
Dim resultText As String = curElement.GetAttribute("id").ToString
If resultText = "something you need to figure out" Then
'Populate the text box
txt_Title.Text = curElement.innerHTML;
End If
Next
End Sub
注意:我没有通过编译器运行或测试它,所以要为一些问题做好准备,但我希望你明白这一点。