【问题标题】:Get website's inner text without webbrowser无需浏览器即可获取网站的内部文本
【发布时间】:2017-01-28 14:54:24
【问题描述】:

我想通过代码获取网站的内部文本。

我已经可以使用下面的代码获取它的内部 html,但我找不到任何获取 URL 内部文本的代码没有 webbrowser。

这段代码在 webbrowser 中从网站获取文本,但我需要同样的东西,只是没有 webbrowser。

Dim sourceString As String = WebBrowser1.Document.Body.InnerText

【问题讨论】:

  • 您的意思是它正在打开一个网络浏览器并且您不想使用网络浏览器(Internet Explorer)?
  • 不,我想通过代码获取网站的文本(不是 html),而不使用 webbrowser
  • 也许您可以使用一个也可以在 api 中导出组件的 webbrower?如果不是,您将不得不使用一些套接字实现,并且很可能还使用类似 openssl 的东西来处理 https。然后它只是一个 get 请求,它将返回所有的 html。
  • 见stackoverflow.com/questions/92522/http-get-in-vb-net第二个答案,投票赞成20。
  • 您是否要收集浏览器中呈现的可读文本以进行 SEO 或字数分析??

标签: html vb.net get httpwebrequest


【解决方案1】:

与HtmlAgilityPack...

Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles ToolStripButton1.Click
    Dim doc As HtmlAgilityPack.HtmlDocument = New HtmlAgilityPack.HtmlDocument
    With New Net.WebClient
        doc.LoadHtml(.DownloadString("https://example.com"))
        .Dispose()
    End With

    Debug.Print(doc.DocumentNode.Name)
    PrintChildNodes(doc.DocumentNode)

    Debug.Print(doc.DocumentNode.Element("html").Element("body").InnerText)
End Sub

Sub PrintChildNodes(Node As HtmlAgilityPack.HtmlNode, Optional Indent As Integer = 1)
    For Each Child As HtmlAgilityPack.HtmlNode In Node.ChildNodes
        Debug.Print("{0}{1}", String.Empty.PadLeft(Indent, vbTab), Child.Name)
        PrintChildNodes(Child, Indent + 1)
    Next
End Sub

【讨论】:

    【解决方案2】:

    **取自** Wolfwyrd

    关于这个问题 HTTP GET in VB.NET

     Try
    Dim fr As System.Net.HttpWebRequest
    Dim targetURI As New Uri("http://whatever.you.want.to.get/file.html")         
    
    fr = DirectCast(HttpWebRequest.Create(targetURI), System.Net.HttpWebRequest)
    If (fr.GetResponse().ContentLength > 0) Then
        Dim str As New System.IO.StreamReader(fr.GetResponse().GetResponseStream())
        Response.Write(str.ReadToEnd())
        str.Close(); 
    End If   
    

    Catch ex As System.Net.WebException '访问资源出错,处理一下 结束尝试

    您将获得 Html 以及 http 标头。不要认为这对https 会起作用。

    【讨论】:

    • 我很确定我提到了 TEXT,而不是 html :)
    猜你喜欢
    • 2012-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-23
    相关资源
    最近更新 更多