使用CefSharp和Javascript实现网络爬虫

2019-12-20 12:17:33 来源:东方头条

打印 放大 缩小

欢迎浏览“使用CefSharp和Javascript实现网络爬虫”,本篇是第3篇。

在爬虫实现过程中,采用Javascript的DOM操作来抓取目标数据,并通过CefSharp提供的Javascript和C#交互方法,把目标数据传递给C#代码进行处理。上一篇介绍了采用Javascript的DOM操作来抓取目标数据,这一篇介绍CefSharp中Javascript和C#之间的调用和交互,以及使用C#委托实现回调处理。

一、CefSharp中Javascript和C#之间的调用和交互

1、C#调用Javascriptprivate ChromiumWebBrowser myBrowser;myBrowser.GetBrowser().MainFrame.ExecuteJavaScriptAsync("console.log("helloworld");");

2、Javascript调用C#,实现数据上报

(1)C#暴露接口定义public class AsyncJavascriptBindingClass{public int ReceiveMsg (int nType, string msg){Console.WriteLine("AsyncJavascriptBindingClass. ReceiveMsg: " + nType + "," + msg);}}

(2)C#暴露接口注册myBrowser.JavascriptObjectRepository.ResolveObject += (_sender, _e) =>{var repo = _e.ObjectRepository;if (_e.ObjectName == "boundAsync"){repo.Register("boundAsync", new AsyncJavascriptBindingClass(), isAsync: true);}};

(3)在Javascript中调用async function sendMsg(type,msg){await CefSharp.BindObjectAsync("boundAsync");var result = await boundAsync. ReceivMsg(type,msg);};sendMsg (1, "hello C#");

二、C#委托使用

当C#代码接收到Javascript传递的目标数据后,常常需要通知其他处理线程进行后续处理,例如通知主界面线程改变当前访问的URL地址等,这里介绍我在实现爬虫过程中使用C#委托实现回调机制。

(1)定义委托类public delegate void CallBackDelegate(string msg);

(2)在AsyncJavascriptBindingClass中触发回调函数public class AsyncJavascriptBindingClass{public CallBackDelegate callBack;public AsyncJavascriptBindingClass(CallBackDelegate _callBack){this.callBack = _callBack;}public int ReceivMsg (int nType, string msg){callback(msg); //todo}}

(3)在主界面对象WindowForm中实现回调函数private void CallBack(string msg){//your code}myBrowser.JavascriptObjectRepository.ResolveObject += (_sender, _e) =>{var repo = _e.ObjectRepository;if (_e.ObjectName == "boundAsync"){repo.Register("boundAsync", new AsyncJavascriptBindingClass(CallBack), isAsync: true);}};

现在我把“使用CefSharp和Javascript实现网络爬虫”的主要知识点进行了记录和分享,供往后查阅。

责任编辑:

相关阅读