gpt4 book ai didi

c# - awesomium 控件未访问 google

转载 作者:行者123 更新时间:2023-11-30 19:40:58 26 4
gpt4 key购买 nike

大家好,

我正在用类中的 awesomium webcontrol 制作一个 Winform 项目。我将该控件导航到 http://www.google.com/(仅用于测试)并向其添加了一个 DocumentReady 甚至监听器。但它不会触发监听器...(我没有收到“在监听器中!”消息)

这是我得到的代码(我在主窗体 (Form1) 中调用了 doSomeScrap):

class scrapper
{
WebControl web = new WebControl();

public void doSomeScrap()
{
MessageBox.Show("in the scrapper...");
web.DocumentReady += webcontrolEventListener;
web.Source = "http://www.google.com".ToUri();
web.Update();
}

private void webcontrolEventListener(object sender, EventArgs e)
{
MessageBox.Show("in the listener!");
}
}

此外,我听说过 LoadingFrameCompleted,但是当我使用它时,出现以下错误:

'Awesomium.Windows.Forms.WebControl' does not contain a definition for 'LoadingFrameCompleted' and no extension method 'LoadingFrameCompleted' accepting a first argument of type 'Awesomium.Windows.Forms.WebControl' could be found (are you missing a using directive or an assembly reference?)

那我做错了什么?或者我忘记了什么来完成这项工作?

额外信息:我有另一个 web 控件,形式为 gui,当我在上面使用这段代码时(没有 update() 调用,控件正在导航......所以我的人是它不是导航,因为它不在 GUI 中。但是我怎样才能让它导航呢?

最佳答案

如果您不需要向用户显示正在抓取的页面,我会使用可以在后台线程上执行的非窗口化 WebView 来执行网页抓取。

在非窗口控制台应用程序中抓取的示例

// Credit: Awesomium v1.7.2 C# Basic Sample
// by Perikles C. Stephanidis
using System;
using Awesomium.Core;
using System.Threading;
using System.Diagnostics;
using System.Reflection;

namespace BasicSample
{
class Program
{
static void Main( string[] args )
{
WebCore.Initialize(WebConfig.Default);

Uri url = new Uri("http://www.google.com");

using ( WebSession session = WebCore.CreateWebSession(WebPreferences.Default) )
{
// WebView implements IDisposable. Here we demonstrate
// wrapping it in a using statement.
using ( WebView view = WebCore.CreateWebView( 1100, 600, session ) )
{
bool finishedLoading = false;
bool finishedResizing = false;

Console.WriteLine( String.Format( "Loading: {0} ...", url ) );

// Load a URL.
view.Source = url;

// This event is fired when a frame in the
// page finished loading.
view.LoadingFrameComplete += ( s, e ) =>
{
Console.WriteLine( String.Format( "Frame Loaded: {0}", e.FrameId ) );

// The main frame usually finishes loading last for a given page load.
if ( e.IsMainFrame )
finishedLoading = true;
};

while ( !finishedLoading )
{
Thread.Sleep( 100 );
// A Console application does not have a synchronization
// context, thus auto-update won't be enabled on WebCore.
// We need to manually call Update here.
WebCore.Update();
}

// Print some more information.
Console.WriteLine( String.Format( "Page Title: {0}", view.Title ) );
Console.WriteLine( String.Format( "Loaded URL: {0}", view.Source ) );
} // Destroy and dispose the view.
} // Release and dispose the session.

// Shut down Awesomium before exiting.
WebCore.Shutdown();

Console.WriteLine("Press any key to exit...");
Console.Read();
}
}
}

使用窗口控件的工作爬虫示例

using System;
using System.Windows.Forms;

namespace App
{
public partial class DemoForm : Form
{
private Awesomium.Windows.Forms.WebControl web;

public DemoForm()
{
InitializeComponent();
doSomeScrap();
}

public void doSomeScrap()
{
MessageBox.Show("in the scrapper...");
web.DocumentReady += webcontrolEventListener;
web.Source = new Uri("http://www.google.com");
web.Update();
}

private void webcontrolEventListener(object sender, EventArgs e)
{
MessageBox.Show("in the listener!");
}

private void InitializeComponent()
{
this.SuspendLayout();
this.web = new Awesomium.Windows.Forms.WebControl();
this.web.Dock = System.Windows.Forms.DockStyle.Fill;
this.Controls.Add(this.web);

this.ClientSize = new System.Drawing.Size(800, 600);
this.Name = "DemoForm";
this.Text = "DemoForm";

this.ResumeLayout(false);
}
}
}

关于c# - awesomium 控件未访问 google,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21500536/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com