问题描述
如何获取此网址并将其粘贴到文本框中?
How can i grab this url and paste it in a textbox?
注销 (Kassy Daniels) · 帮助
Logout (Kassy Daniels) · Help
从此在 webbrowser1
from this in webbrowser1
我想让它获取/logout.php?url 因为每个用户都不一样
i want it to grab the /logout.php? url cause each user is different
当您点击按钮时,只需将其粘贴到 textbox1 中
and just paste it in a textbox1 when you hit a button
推荐答案
假设你的浏览器是 WebBrowser1
,
Assuming your webbrowser is WebBrowser1
,
使用 WebBrowser1.Document.Links
将获得页面上所有链接的 HTMLElementCollection
.
Using WebBrowser1.Document.Links
will get you a HTMLElementCollection
of all of the links on the page.
您可以遍历这些链接,并检查每个链接的 OuterHtml
元素(它将为您提供一个包含原始链接、ID 等的字符串)以获取您的文本寻找(在这种情况下注销?
).
You can iterate through those, and check the OuterHtml
element of each of those links (which will give you a string that contains the raw link, the IDs, etc.) for the text your are looking for (in this case logout?
).
使用 substring 命令,提取该 URL,并消除周围的文本,然后您可以将其直接插入文本框.
Using the substring command, extract that URL, and eliminate the surrounding text, and you can plug that right into the textbox.
更新:使用 Firebug 或者你最喜欢的 DOM 工具,你可以发现注销链接在文档的根 div 中
Update: Using Firebug or your favorite DOM tool, you can find that the logout link is in the root div of the document
Dim rootDiv As HtmlElement = WebBrowser1.Document.GetElementById("root")
Dim links As HtmlElementCollection = rootDiv.GetElementsByTagName("a")
Dim index As Integer
For i As Integer = 0 To links.Count - 1
If links(i).InnerHtml.Contains("Logout") Then
index = i
Exit For
End If
Next i
Dim stringofLink As String = links(index).OuterHtml
如果页面结构发生变化,这应该对其具有一定的弹性,但您可能需要进一步概括它.处理 stringofLink 无论如何你想打破它(找到你需要的&然后从问号复制到那个点).
If the page changes in structure, this should be somewhat resilient to it, but you may need to generalize it further. Process stringofLink however you want to break it up (find the ampersand that you need and then copy from the question mark to that point).
这篇关于VB.NET 从 Webbrowser1 抓取 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!