C#使用CefSharp控件實(shí)現(xiàn)爬蟲
一、CefSharp介紹
CEF 全稱是Chromium Embedded Framework(Chromium嵌入式框架),是個基于Google Chromium項(xiàng)目的開源Web browser控件,支持Windows, Linux, Mac平臺。CEFSharp就是CEF的C#移植版本。
就是一款.Net編寫的瀏覽器包,方便你在Winform和WPF中內(nèi)嵌的Chrome瀏覽器組件
GitHub地址:https://github.com/cefsharp/CefSharp
安裝
使用Nuget包引用


把項(xiàng)目改成64位


切換到X64

安裝完之后工具欄應(yīng)該會多出來這個控件(直接拖動用不了!)

二、使用
1、獲得頁面源代碼
注意:
1、GetSourceAsync獲取源碼的方法是異步操作
2、判斷頁面加載完成,會觸發(fā)FrameLoadEnd頁面加載完成事件。使用CEF無法確定一個網(wǎng)站是否已經(jīng)完全加載完成,我們只能在它每一次加載完成時,處理它的頁面源碼。(如果需要主動等待網(wǎng)站加載完成,可以試試使用Selenium)
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ChromiumWebBrowser WebBrowser;
private void Form1_Load(object sender, EventArgs e)
{
var settings = new CefSettings()
{
UserAgent = "Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Mobile Safari/537.36",
};
//Perform dependency check to make sure all relevant resources are in our output directory.
Cef.Initialize(settings, performDependencyCheck: true, browserProcessHandler: null);
// cefsharp提供的瀏覽器控件,一般用它充滿窗口就搞定了
WebBrowser = new ChromiumWebBrowser("http://www.163.com")
{
// 填充整個父控件
Dock = DockStyle.Fill
};
WebBrowser.FrameLoadEnd += new EventHandler<FrameLoadEndEventArgs>(FrameEndFunc);
// 添加到窗口的控件列表中
this.panel1.Controls.Add(WebBrowser);
}
private void FrameEndFunc(object sender, FrameLoadEndEventArgs e)
{
MessageBox.Show("加載完畢");
this.BeginInvoke(new Action(() =>
{
String html = WebBrowser.GetSourceAsync().Result;
richTextBox1.Text = html;
}));
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
// 結(jié)束時要銷毀
Cef.Shutdown();
}
}
}效果:可以加載很多原生webbrowser不能加載的內(nèi)容 可以適應(yīng)iframe

2、執(zhí)行頁面中的js函數(shù)
測試的js代碼
<html>
<body>
<button type="button" onclick="test(1,2)">測試按鈕</button>
</body>
<script type="text/javascript">
function test(a,b)
{
var c = testfunc(a,b);
alert(c);
}
function testfunc(a,b)
{
return a+b;
}
</script>
<html>調(diào)用頁面中的testfunc函數(shù)
private void button3_Click(object sender, EventArgs e)
{
using (StreamReader sr = new StreamReader("JavaScript1.html"))
{
string html = sr.ReadToEnd();
WebBrowser.LoadHtml(html, "http://testpage/");
}
}
private void button4_Click(object sender, EventArgs e)
{
String script = "testfunc(99,1)";
var result = this.WebBrowser.EvaluateScriptAsync(script).Result.Result;
MessageBox.Show(result.ToString());
}效果

3、常用方法
//瀏覽網(wǎng)址:
WebBrowser = new ChromiumWebBrowser("https://www.baidu.com");
// 或
WebBrowser.Load("https://www.baidu.com");
// 獲取HTML(整體):
WebBrowser.GetSourceAsync().Result;
// 獲取HTML(特定Frame):
WebBrowser.GetBrowser().GetFrame(“SI2_mem_index”).GetSourceAsync().Result;
//執(zhí)行網(wǎng)頁上的JavaScript:
ExecuteJavaScriptAsync("document.getElementById('username').onkeydown();");
//模擬左鍵點(diǎn)擊:
WebBrowser.GetBrowser().GetHost().SendMouseClickEvent(x, y, MouseButtonType.Left, false, 1, CefEventFlags.None);
Thread.Sleep(50);
WebBrowser.GetBrowser().GetHost().SendMouseClickEvent(x, y, MouseButtonType.Left, true, 1, CefEventFlags.None);實(shí)例地址:https://github.com/zhaotianff/CSharpCrawler
到此這篇關(guān)于C#使用CefSharp控件實(shí)現(xiàn)爬蟲的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
.NET連接MongoDB數(shù)據(jù)庫實(shí)例教程
這則小竅門將講述如何開發(fā)一個.NET應(yīng)用來連接Mongo數(shù)據(jù)庫并執(zhí)行多種操作。同時還稍微涉及了Mongo數(shù)據(jù)庫和多種命令2013-11-11
Ruby創(chuàng)建數(shù)組方法總結(jié)
在本篇文章里小編給大家分享了關(guān)于Ruby創(chuàng)建數(shù)組方法的知識點(diǎn)內(nèi)容,對戲有興趣的朋友們學(xué)習(xí)下。2019-01-01
C# DropDownList中點(diǎn)擊打開新窗口的方法
C# DropDownList中點(diǎn)擊打開新窗口的方法,需要的朋友可以參考一下2013-03-03
C# Process調(diào)用外部程序的實(shí)現(xiàn)
這篇文章主要介紹了C# Process調(diào)用外部程序的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02

