vb.net VB从html元素获取文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17934566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:27:01  来源:igfitidea点击:

VB get text from html element

vb.net

提问by user2620851

I need to get the text between two span tags on a web page using visual basic.

我需要使用visual basic获取网页上两个span标签之间的文本。

<span>Some Text</span>

I know there must be a way but I can't seem to find it.

我知道一定有办法,但我似乎找不到。

This is for a website i do not own.

这是一个我不拥有的网站。

回答by Yuriy Galanter

Give your span an ID and runat="server"attribute e.g.

给你的跨度一个 ID 和runat="server"属性,例如

<span id="xMySpan" runat="server">Some Text</span>

Then you will be able to retrieve it in server-side code, e.g.

然后您将能够在服务器端代码中检索它,例如

Dim sVar As String = xMySpan.InnerHtml

回答by eclipsis

What about assigning an ID to the span? If you do, then this works:

为跨度分配一个 ID 怎么样?如果你这样做,那么这有效:

TextBox1.Text = _
 WebBrowser1.Document.GetElementById("spanID").GetAttribute("innerText")

Using this format:

使用这种格式:

<span id="spanID">...</span>

EDIT: To filter by content:

编辑:按内容过滤:

$("span").filter(function(){
    return $(this).html() == "a";
})

Will work with this:

将与此一起使用:

<span>a</span>

回答by Louis

Are you extracting this from the entire HTML document or just the quoted text above?

你是从整个 HTML 文档中提取这个还是只从上面引用的文本中提取?

If its just the above (and you've already filtered out the other HTML) then you can use a conbination of LEFT() and RIGHT() to snip off the ends, or use REPLACE() to get rid of the two tags.

如果它只是上面的(并且您已经过滤掉了其他 HTML),那么您可以使用 LEFT() 和 RIGHT() 的组合来剪掉末端,或者使用 REPLACE() 来去掉这两个标签。

回答by dluna88

I made this script, hope it will be helpful

我做了这个脚本,希望它会有所帮助

I have:

我有:

  • Textbox to get the youtube url [urlVideo]

  • Button to load the page [btn_loadViews]

  • A webBrowser Control [webBrowser1]

  • and a label to show the text [lb_views]

  • 获取 youtube url [urlVideo] 的文本框

  • 加载页面的按钮 [btn_loadViews]

  • 一个 webBrowser 控件 [webBrowser1]

  • 和一个标签来显示文本 [lb_views]

I'm not validating anything, so This is just an example of how do i get text from websites. If there's another way to do it, i would like to know it too. =)

我没有验证任何东西,所以这只是我如何从网站获取文本的一个例子。如果有其他方法可以做到,我也想知道。=)

Private Sub btn_loadViews_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_loadViews.Click

    WebBrowser1.Navigate(urlVideo.Text)
    WaitForPageLoad()

    getViews()


End Sub

Private Sub getViews()
    Try
        Dim version = FileVersionInfo.GetVersionInfo("c:\windows\system32\ieframe.dll")
        'Depending on the navigator version, google's server sends diffetent pages, so
        'Here Detect ie version
        If version.ProductVersion < "8" Then
            lb_views.Text = WebBrowser1.Document.GetElementById("vc").FirstChild.InnerText
        Else
            lb_views.Text = WebBrowser1.Document.GetElementById("watch7-views-info").FirstChild.InnerText
        End If

    Catch ex As Exception
        MsgBox(ex.ToString)
        Application.Exit()
    End Try

End Sub


Private Property pageready As Boolean = False

Private Sub WaitForPageLoad()
    AddHandler WebBrowser1.DocumentCompleted, New     WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
    While Not pageready
        Application.DoEvents()
    End While
    pageready = False
End Sub

Private Sub PageWaiter(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
    If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
        pageready = True
        RemoveHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
    End If
End Sub

回答by user12291748

Dim WithEvents hDoc As HTMLDocument
Set hDoc = WebBrowser1.Document
Dim strValue As String
strValue = hDoc.getElementsByName("so").Item(0).Value