本程序基於CefSharp實現自動打開一個網站,自動輸入帳號密碼自動註冊。而後跳轉到商品頁自動輸入評論的內容而後提交。徹底模擬人爲在瀏覽器的全部操做,包括自動輸入,自動點擊等操做。html
本解決方案能夠應用於網絡爬蟲,刷單,刷評論,參與自動秒殺活動。搶票程序等等。web
最終效果以下:json
cefsharp是一個在c#中使用Chrome瀏覽器內核實現瀏覽器功能的插件,相似於c#中的WebBrowser功能。c#
主要代碼:瀏覽器
一、引用cookie
<wpf:ChromiumWebBrowser x:Name="webBrowser" Address="https://www.walmart.com/account/login"/>網絡
二、執行JavaScriptapp
public async void EvaluateJavaScript(string s, bool addCommonJs = false) { try { if (addCommonJs) { s = commonJs + s; } var response = await webBrowser.EvaluateScriptAsync(s); if (response.Success && response.Result is IJavascriptCallback) { response = await ((IJavascriptCallback)response.Result).ExecuteAsync("This is a callback from EvaluateJavaScript"); } var EvaluateJavaScriptResult = response.Success ? (response.Result ?? "null") : response.Message; } catch (Exception e) { MessageBox.Show("Error while evaluating Javascript: " + e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
三、設置cookieasync
public static void SetCefCookies(string url, CookieCollection cookies) { //Cef.GetGlobalCookieManager().SetStoragePath(Environment.CurrentDirectory, true); foreach (System.Net.Cookie c in cookies) { var cookie = new CefSharp.Cookie { Creation = DateTime.Now, Domain = c.Domain, Name = c.Name, Value = c.Value, Expires = c.Expires }; Task<bool> task = Cef.GetGlobalCookieManager().SetCookieAsync(url, cookie); while (!task.IsCompleted) { continue; } bool b = task.Result; } }
四、獲取當前頁面的html源碼ide
private string GetHTMLFromWebBrowser() { // call the ViewSource method which will open up notepad and display the html. // this is just so I can compare it to the html returned in GetSourceAsync() // This is displaying all the html code (including child frames) //webBrowser.GetBrowser().MainFrame.ViewSource(); // Get the html source code from the main Frame. // This is displaying only code in the main frame and not any child frames of it. Task<String> taskHtml = webBrowser.GetBrowser().MainFrame.GetSourceAsync(); string response = taskHtml.Result; return response; }
五、攔截json請求結果
public class DefaultResourceHandler : ResourceRequestHandler { public event EventHandler<JsonResponseHandlerEventArgs> json_response_handler; private Dictionary<ulong, MemoryStreamResponseFilter> responseDictionary = new Dictionary<ulong, MemoryStreamResponseFilter>(); public DefaultResourceHandler() { } public DefaultResourceHandler(EventHandler<JsonResponseHandlerEventArgs> json_response_handler) { this.json_response_handler = json_response_handler; } protected override IResponseFilter GetResourceResponseFilter(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response) { if (response.MimeType.Equals("application/json", StringComparison.OrdinalIgnoreCase) || (response.Headers["Content-Type"] != null && response.Headers["Content-Type"].ToLower().Contains("application/json"))) { return JsonResponseFilter.CreateFilter(request.Identifier.ToString()); } return null; } protected override void OnResourceLoadComplete(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response, UrlRequestStatus status, long receivedContentLength) { var filter = JsonResponseFilter.GetFileter(request.Identifier.ToString()) as JsonResponseFilter; if (filter != null) { var encode = !string.IsNullOrEmpty(response.Charset) ? Encoding.GetEncoding(response.Charset) : Encoding.UTF8; using (var read = new StreamReader(filter.GetStream(), encode)) { //獲取到的json內容 var text = read.ReadToEnd(); json_response_handler?.Invoke(response, new JsonResponseHandlerEventArgs(request, response, text)); Trace.WriteLine(response.MimeType + "=>" + request.Url + "::" + text); } } } }