添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Public Class Form1 '定义cef浏览器控件的全局变量,方便后面调用,注意WithEvents Private WithEvents browser As ChromiumWebBrowser Public Sub New () ' 此调用是设计器所必需的。 InitializeComponent() ' 设置cef控件菜单语言为中文 Dim settings As New CefSettings() With { .Locale = "zh-CN" CefSharp.Cef.Initialize(settings) '初始化cef浏览器控件样式和默认打开的地址,并将其加入到Panel控件中 browser = New ChromiumWebBrowser( "about:blank" ) With { .Dock = DockStyle.Fill Panel2.Controls.Add(browser) '手动跳转新的地址 browser.Load( "http://mail.qq.com" ) End Sub Private Sub Button1_Click(sender As Object , e As EventArgs) Handles Button1.Click '从textbox中获取内容当做url扔给cef访问 Dim url = TextBox1. Text browser.Load(url) End Sub Private Sub Button2_Click(sender As Object , e As EventArgs) Handles Button2.Click '调用cef执行js代码并返回结果,这里使用js原生函数querySelector通过css选择器获取dom的值 Dim js As String = "document.querySelector('.login_pictures_txt').innerText" Dim html_task As Task( Of JavascriptResponse) = browser.EvaluateScriptAsync(js, timeout:= Nothing ) '阻塞等待task任务结束 html_task.Wait() RichTextBox1. Text = html_task.Result.Result End Sub End Class

获取异步加载页面的HTML代码

比较简单粗暴有效的方法:使用Timer循环在页面中执行js通过css选择器去找加载完成后才会出现的dom元素个数,大于0则可认定为页面加载完成了.

Imports CefSharp
Imports CefSharp.WinForms
Public Class SingleBrowser    
    Private browser As ChromiumWebBrowser
    Private WithEvents get_html_tm As New Timer() With {.Interval = 200}    '获取页面HTML的计时器
    Public Sub New()
        ' 此调用是设计器所必需的。
        InitializeComponent()
        ' 在 InitializeComponent() 调用之后添加任何初始化。
        Dim settings As New CefSettings With {
            .Locale = "zh-CN",
            .CachePath = Application.StartupPath & "\Cache"
        CefSharp.Cef.Initialize(settings)
        browser = New ChromiumWebBrowser("about:blank") With {
                .Dock = DockStyle.Fill
        Panel2.Controls.Add(browser)
    End Sub
    Private Sub startBtn_Click(sender As Object, e As EventArgs) Handles startBtn.Click
        '处理URL路由
        Dim url As String = url_textbox.Text
		'加载文本框中的地址,比如淘宝的搜索结果页面
        browser.Load(url)
        '开始计时器工作
        get_html_tm.Start()
    End Sub
    ''' <summary>
    ''' 判断淘宝搜索结果页面是否加载完毕
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Async Sub get_html_tick(sender As Object, e As EventArgs) Handles get_html_tm.Tick
        Dim js As String = "document.querySelectorAll('.pic-link').length"
        Dim result As Integer = doJs(js)
        If result > 0 Then
            get_html_tm.Stop()
            '加载完毕,开始获取HTML内容
            Dim html As String = Await browser.GetSourceAsync()
            Console.WriteLine("----加载HTML成功")
            Console.Write(html)
        End If
    End Sub
    ''' <summary>
    ''' 在浏览器中执行js
    ''' </summary>
    ''' <param name="js"></param>
    ''' <returns>返回js执行结果,object对象或者nothing</returns>
    Private Function doJs(ByVal js As String)
        If Not browser.IsLoading Then
            Dim js_task As Task(Of JavascriptResponse) = browser.EvaluateScriptAsync(js)
            js_task.Wait()
            Return js_task.Result.Result
            Return Nothing
        End If
    End Function
End Class

使用AngleSharp解析HTML

Imports AngleSharp
Public Class SingleBrowser
    Private parser As New AngleSharp.Parser.Html.HtmlParser()   'HTML解析器
    Private Sub dealHtml(Byval html as String)
		'解析HTML为AngleSharp的IHtmlDocument对象
    	Dim doc = parser.Parse(html)
    	'通过css选择器筛选出符合条件的dom元素集合
    	Dim link_list = doc.QuerySelectorAll(".pic-link")
    	'遍历dom集合进行处理
    	For Each link In link_list
    		'获取dom的文本内容
    		Console.WriteLine(link.TextContent())
    		'获取dom的属性href
    		Console.WriteLine(link.GetAttribute("href"))
    End Sub

使用offscreen在后台隐式获取指定页面的HTML

注意:需要另外从NuGet上安装CefSharp.Offscreen的包包

Imports CefSharp
Imports CefSharp.OffScreen
Public Class offscreen
    Private browser As ChromiumWebBrowser
    Public Sub New()
        ' 此调用是设计器所必需的。
        InitializeComponent()
        ' 允许多线程间修改主UI线程的控件内容
        CheckForIllegalCrossThreadCalls = False
        Dim settings As New CefSettings() With {
        .Locale = "zh-CN"
        CefSharp.Cef.Initialize(settings)
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            browser = New ChromiumWebBrowser("mail.qq.com")
            AddHandler browser.LoadingStateChanged, AddressOf doneLoad
        Catch ex As Exception
            TextBox1.Text = ex.Message
        End Try
    End Sub
    ''' <summary>
    ''' 处理browser对象加载状态发生改变时的事件
    ''' 注意括号中的参数必须与browser.LoadingStateChanged签名一致
    ''' 所谓签名一致,就是括号里面参数的数量、类型一致(不知道这样理解对不对)    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Async Sub doneLoad(ByVal sender As Object, ByVal e As LoadingStateChangedEventArgs)
        '如果browser不是处于loading状态
        If Not e.IsLoading Then
            '移除绑定的当前事件(即当前事件只触发一次)
            RemoveHandler browser.LoadingStateChanged, AddressOf doneLoad
            '手动设置的等待1秒,让browser渲染完毕
            Threading.Thread.Sleep(1000)
            '阻塞等待(异步)获取browser中的源代码
            Dim html As String = Await browser.GetSourceAsync()
            TextBox1.Text = html
        End If
    End Sub
End Class
复制代码
  •