快捷搜索:  汽车  科技

javascript python 爬虫(使用CefSharp和Javascript实现网络爬虫-Javascript和C)

javascript python 爬虫(使用CefSharp和Javascript实现网络爬虫-Javascript和C)async function sendMsg(type msg){await  CefSharp.BindObjectAsync('boundAsync');var result = await boundAsync. ReceivMsg(type msg);};sendMsg (1 'hello  C#'); myBrowser.JavascriptObjectRepository.ResolveObject = (_sender _e) =>{ var repo = _e.ObjectRepository;if (_e.ObjectName == "boundAsync"){repo.Register("boundAsync" new AsyncJavascriptBindingClass() isAsyn

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

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

javascript python 爬虫(使用CefSharp和Javascript实现网络爬虫-Javascript和C)(1)

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

1、C#调用Javascript

private 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实现网络爬虫”的主要知识点进行了记录和分享,供往后查阅。

猜您喜欢: